Flurl: Configure which HTTP status codes throw exceptions

Created on 5 Feb 2015  路  6Comments  路  Source: tmenier/Flurl

Currently any non-2XX response throws a FlurlHttpException. (The reasoning is that there's no other mechanism to capture a failed status in a convenience method like this: url.GetJsonAsync<T>())

It's been suggested to allow configuring other non-exception-thrown statuses. I'm considering something along these lines:

C# AllowHttpStatuses("202,206,400-499") AllowAnyHttpStatus()

These would be methods on FlurlClient and would of course be chainable. Comments/suggestions welcomed!

Most helpful comment

Ok, let's try this again. You can now set patterns at the individual call/client level. Tests to demonstrate:

``` c#
[Test, ExpectedException(typeof(FlurlHttpException))]
public async Task fails_on_non_success_status() {
await "http://httpbin.org/status/418".GetAsync();
}

[Test]
public async Task can_allow_non_success_status() {
await "http://httpbin.org/status/418".AllowHttpStatus("4xx").GetAsync();
}

You can also allow anything:

``` c#
var result = url.AllowAnyHttpStatus().GetAsync();

If you're reusing the client and need to clear them (including the global config setting, but just for this client instance):

c# myFlurlClient.AllowedHttpStatusRanges.Clear();

None of this has any effect on 2XX statuses - they'll never throw.

This is pushed to v0.5.2 on NuGet. Let me know your thoughts.

All 6 comments

:+1:

Apologies for the delay. I've opted to go with just pattern (string) based approach for setting this as it allows for the most flexibility. As of Flurl.Http 0.5.1, this is configurable globally like so:

C# FlurlHttp.Configure(c => { c.AllowedHttpStatusRange = "100-299,4xx"; });

To suppress exceptions in all cases, set to "*".

I anticipate this may lead to requests for making this configurable per call. Let me know if that's something you need.

I anticipate this may lead to requests for making this configurable per call. Let me know if that's something you need.

Yes please, per-call configuration would be helpful as most of the time the built-in exception throwing is appreciated (like when using the Json extension methods). It's only in certain cases where I may be checking for the existence of an object/route where I would be manually checking the response code and need to allow it to not throw on a 404, etc.

Noted, I'll re-open this. I agree that it's a much more useful/complete feature if you can control it per call.

Ok, let's try this again. You can now set patterns at the individual call/client level. Tests to demonstrate:

``` c#
[Test, ExpectedException(typeof(FlurlHttpException))]
public async Task fails_on_non_success_status() {
await "http://httpbin.org/status/418".GetAsync();
}

[Test]
public async Task can_allow_non_success_status() {
await "http://httpbin.org/status/418".AllowHttpStatus("4xx").GetAsync();
}

You can also allow anything:

``` c#
var result = url.AllowAnyHttpStatus().GetAsync();

If you're reusing the client and need to clear them (including the global config setting, but just for this client instance):

c# myFlurlClient.AllowedHttpStatusRanges.Clear();

None of this has any effect on 2XX statuses - they'll never throw.

This is pushed to v0.5.2 on NuGet. Let me know your thoughts.

Thanks, those changes look good!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

voroninp picture voroninp  路  5Comments

tmenier picture tmenier  路  4Comments

julealgon picture julealgon  路  6Comments

tomasr78 picture tomasr78  路  7Comments

dagostinelli picture dagostinelli  路  5Comments