Skip to content

StatusCode in HttpRequestException #23648

@vanillajonathan

Description

@vanillajonathan

When the EnsureSuccessStatusCode method in the HttpResponseMessage throws a HttpRequestException exception it is not possible to determine the status code.

Add a new property named StatusCode to the HttpRequestException class which gets set by the EnsureSuccessStatusCode method of the HttpResponseMessage class.

Rationale and Usage

Allows you to determine the status code which triggered the exception.

try
{
    DoSomeHttp();
}
catch (HttpRequestException e)
{
    Log($"Request failed with status code: {e.StatusCode}.");
}

private void DoSomeHttp()
{
    // ...
    message.EnsureSuccessStatusCode();
}

Proposed API

public class HttpRequestException : Exception
{
    // ...
    public HttpRequestException(string message, StatusCode statusCode);
    public HttpRequestException(string message, Exception inner, StatusCode statusCode);
    // ...
    public StatusCode StatusCode { get; }
    // ...
}

Open Questions

Should the property have a setter?
Should the exception have a constructor that sets the property?
Should the property be nullable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions