WebRequest and Node.js - error handling - c#

What would be the correct way of handling errors in communication between a C# client app and Node.js server? My solution was to send status code 400 or 500 and error message in the response body, but WebRequest seems to not receive the body message when status code implies an error. I need to read this error message to show for the user the correct info about the error.
I guess typically server would throw WebRequestException which could pass the error message, however I have no idea how to throw such error from the Node.js app.
I cannot change the status code to 200 and read error from the body because previous versions of the app would not handle it correctly.

Well, I figured it out. In the client, in the catch block, when WebRequestException is thrown because of the error status code, you can still begin receiving the response (the response is in this exception).
My bad. :)

Related

Http 500 in IIS Log when i throw a c# exception

Is there any possibility to throw an exception in the code of a web application which would not cause error 500 in the IIS logs?
500 means internal server error, i.e. your application have failed.
I'm guessing that you are asking for some sort of indication when the client have done something wrong.
For MVC 5.x there is an HttpException which can be used to return other error codes. (500 is returned for all exceptions but HttpException)
However! Exceptions are application failures. Your application does not fail just because the client sent something invalid.
Instead, you should send back a HttpResponse with the correct HTTP code. For MVC applications you can use the HttpStatusCodeResult in the controller.

web api returns a json error but visual studio only sees it as status code 500

I'm getting a 500 status code back in visual studio for an api shown below although when using PostMan with the same credentials I get a json response back saying the token has expired. Is this because I'm not seeing the response back as json or something? Obviously I need to handle if the token has expired so getting 500 back is not sufficient. Many thanks,
Code failing with status code 500
Same call but using PostMan
use WebException rather than Exception.

Customize WCF Rest error responses

I have written a WCF REST API for third-party use. One of the things I want to do is to return custom error responses to clients if anything goes wrong. I don't want the WCF default error page showing that internal server error has occurred or method name not found.
To do so I throw WebFaultException<Error> where necessary. This return the following type of response to the client:
<Error>
<type>MissingTag</type>
<Desc>Tag 349 is missing</Desc>
</Error>
But how can I handle if any other type of error occurs like a serialization error or the "Method not found" error or place where I want to check that POST, PUT and PATCH have http header content-type present. I want to throw WebFaultException<> there too. I tried looking into IErrorHandler but could not get it working.
Any one got ideas on how to implement this type of thing. Also can I have a simple code demonstrating the IErrorHandler usage?
You can look into Message Inspectors BeforeSendReply for customizing the reply that needs to be sent to the client
If the content-type is not set when the request is made you can look into the AfterReceiveRequest where in you can customise the request received and then action as needed.

Client found response content type of 'application/octet-stream', but expected 'text/xml'

One of our clients is causing this error when we call their web service. I'm trying to figure out how to help them. I'm not sure if they are sending the MIME-type wrong specifically, or if the error is being caused because the response begins with the "HTTP/1.1 200 OK" instead of beginning immediately with <?xml>. I don't know too much about this client/service binding.
Exception captured is:
"Client found response content type of 'application/octet-stream', but expected 'text/xml'. The request failed with the error message: -- HTTP/1.1 200 OK Content-Type:text/xml; charset=utf-8 Content-Length:1056"
The rest of the exception lists the entire xml, soap:envelope, soap:body and actual object tags that looks perfectly fine.
Our call is made by .NET 1.1.

Is there a way to retrieve the message body when the server returns a 500 internal Server Error?

I have a routine that submits a SOAP request using HttpWebRequest and WebResponse. If the SOAP Request fails the server sends back HTTP/1.1 500 Internal Server Error. When I trap the error I have yet to find a way to view the body of the reply which contains the fault code.
Is there a way to retrieve the message body when the server returns a 500 internal Server Error?
In body of the reply which I am not able to retrieve.
faultstring xml:lang="en-US" Specified argument was out of the range of valid values.
I haven't time to test this, but a WebException will be thrown in this situation.
You can get access to the error response via the WebException.Response property.
You will then be able to access information from the respons, e.g. you could try calling WebException.Response.GetResponseStream() to get the body of the response.

Categories