Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flurl.Http custom error

I am making an application where a client does requests to a server. The server is written in node.js and the client in .NET using flurl.Http.

When a request on the server fails, it is always usefull to create a custom error message. Something like this:

request.respond(500, { message: "targetid is mandatory" });

However this invokes a FlurlHttpException on the client side and the JSON information in the response gets lost.

How can I receive this JSON information when a non-successful response code is received from the server?

like image 283
Cerebres Avatar asked Dec 29 '14 09:12

Cerebres


1 Answers

Since Flurl sort of specializes in getting from a URL to a deserialized response body in as few keystrokes as possible, it's often asked how to deal with error responses, where the body almost always take a different shape than successful ones. This is why Flurl defaults to throwing on non-2XX responses, so you can deal with error responses like this:

try {
    var t = await "http://api.com".GetJsonAsync<T>();
}
catch (FlurlHttpException ex) {
    var error = await ex.GetResponseJsonAsync<TError>();
}

There are a few variations:

ex.GetResponseStringAsync();
ex.GetResponseJsonAsync(); // returns a dynamic
like image 63
Todd Menier Avatar answered Oct 08 '22 12:10

Todd Menier