8000 Throw exceptions for failure status codes with more data about the response in them. by bennor · Pull Request #45 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Throw exceptions for failure status codes with more data about the response in them. #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2014
Merged

Conversation

bennor
Copy link
Contributor
@bennor bennor commented Jun 28, 2014

Some APIs return useful messages and other data in 401 responses, etc, but HttpResponseMessage.EnsureStatusCode() eats the response, and only includes the status code in the message of the exception it throws.

This PR adds (and throws) a custom exception type with enough information in it to deal with the error properly in calling code.

I was able to achieve this in my own project using a custom message handler that intercepts the response and throws a custom exception, but it feels like a lot of work and a bit of a hack to have to do that.

bennor added 2 commits June 28, 2014 20:37
Some APIs return useful messages in 401 responses, etc, but
HttpResponseMessage.EnsureStatusCode() throws away the response, and
only includes the status code in the message of the exception it throws.

This throws a custom ApiException with enough information in it to deal
with the error properly.
Had to modify the PostToRequestBin test too, but I'm not sure what the
point of that is since it will always fail. Maybe we should just remove
it?
if (response.Content == null) return exception;

exception.ContentHeaders = response.Content.Headers;
exception.Content = await response.Content.ReadAsStringAsync();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Content can be null. Also, what happens if reading the content fails? We should still return an ApiException with the status code information

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Content being null is checked up on line 551 (before trying to read the headers). But yeah, this will 💥 if it fails to read the content.

Do you reckon I should just wrap this line in a try/catch with an empty catch? It feels a bit dirty, but we're already handling an exception at this point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bennor Yep, that's what I would do - the failure content is just bonus points, we can tolerate if it fails

@bennor
Copy link
Contributor Author
bennor commented Jul 6, 2014

Hey, no hurry on this obviously, but just in case you missed the notification - I pushed some updates about 6 days ago. :trollface:

8000
public HttpContentHeaders ContentHeaders { get; private set; }
public string Content { get; private set; }
public bool HasContent
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⬆️

@anaisbetts
Copy link
Member

Cool, I'm gonna fix up these small things and merge. Thanks @bennor!

@anaisbetts anaisbetts merged commit e7c53ad into reactiveui:master Jul 6, 2014
@bennor
Copy link
Contributor Author
bennor commented Jul 6, 2014

No problem. Sorry about that style stuff. I tried to set up Resharper to match but a few of the finer points are beyond what Resharper can do for me and it's a fair but different to the style I use elsewhere. I'll make sure I double check it all next time. 😸

@anaisbetts
841C Copy link
Member

@bennor No worries, I fixed them up on merge. Also, this is why I don't use R#, because some of its style decisions are hard-coded. Also the death-by-1000-paper-cuts perf issues

@bennor bennor deleted the custom-exception branch October 12, 2014 20:38
@lock lock bot locked and limited conversation to collaborators Jun 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0