Posts

Showing posts with the label exception-handling

What is the proper way to catch and identify the HttpRequestException “The remote name could not be resolved: 'www.example.com'”?

What is the proper way to catch and identify the HttpRequestException “The remote name could not be resolved: 'www.example.com'”? I want to be able to catch and identify the exceptions that belong to this specific type and then return a suitable error message. What is the proper way to do that in a catch block? 2 Answers 2 The exception you need to catch is an HttpRequestException specifically with an InnerException that is a WebException and has a Status property with a value of WebExceptionStatus.NameResolutionFailure . HttpRequestException InnerException WebException Status WebExceptionStatus.NameResolutionFailure Fortunately, using C# 6.0 exception filters, it's now easy to catch only an exception that fulfills these specific criteria: var hc=new HttpClient(); try { (await hc.GetStringAsync("https://www.googggle.com")); } catch(HttpRequestException ex) when ((ex....

Ensure that a specific exception always results in a given HTTP response status code

Ensure that a specific exception always results in a given HTTP response status code Requirement I have an ASP.Net MVC application that works with a number of different libraries. As with most libraries, various function calls may cause one of many different exceptions to be thrown. Currently, whenever any exception is thrown, then MVC application 'handles' them and returns an "internal server error" (code 500) back to the client (eg. web browser). This is fine for most cases, however, there is one specific exception type (in this case UnauthorizedAccessException ) that I would like to result in an "Unauthorized" (code 401) status being sent in the response, instead of the usual 500 error. UnauthorizedAccessException Current Attempt I did a fair bit of research and it looks like the bets way to 'catch' all exceptions and process them is by using the Application_Error method. So I tried the following implementation of Application_Error in the MvcApp...

API Gateway + Lambda failed to handle exception

Image
API Gateway + Lambda failed to handle exception I have Python based Lambda which is integrated with API Gateway and try to handle lambda exception below. { "stackTrace": [ [ "/var/task/index.py", 14, "handler", "raise Exception(err)" ] ], "errorType": "Exception", "errorMessage": "device name existed" } I've defined HTTP status 400 of Method Response for the exception. I also defined "Lambda Error Regex" and "Body Mapping Templates". But the API Gateway still responses HTTP status 200 instead of 400 when my lambda raised an exception. Anyone has idea what's going wrong. Did I miss something? 1 Answer 1 I found the solution by myself. Here is my Lambda code: raise Exception({ "errorType" : "Exception", "httpS...