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!
:+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!
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();
}
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.