I'm writing a RESTful API in Web API and I'm not sure how to handle errors effectively. I want the API to return JSON, and it needs to consist of the exact same format every single time - even on errors. Here are a couple of examples of what a successful and a failed response might look like.
Success:
{
Status: 0,
Message: "Success",
Data: {...}
}
Error:
{
Status: 1,
Message: "An error occurred!",
Data: null
}
If there is an exception - any exception at all, I want to return a response that is formed like the second one. What is the foolproof way to do this, so that no exceptions are left unhandled?
Implement IExceptionHandler.
Something like:
public class APIErrorHandler : IExceptionHandler
{
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
var customObject = new CustomObject
{
Message = new { Message = context.Exception.Message },
Status = ... // whatever,
Data = ... // whatever
};
//Necessary to return Json
var jsonType = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
var response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, customObject, jsonType);
context.Result = new ResponseMessageResult(response);
return Task.FromResult(0);
}
}
and in the configuration section of WebAPI (public static void Register(HttpConfiguration config)) write:
config.Services.Replace(typeof(IExceptionHandler), new APIErrorHandler());
Related
I'm having a bit of difficulty with an AWS issue that doesn't seem to have that great of documentation.
I have my lambda function here:
public async Task<string> FunctionHandler(ConnectRequest request, ILambdaContext context)
{
AmazonLexClient lexClient = new AmazonLexClient();
var response = new PostTextResponse();
PostTextRequest postRequest = new PostTextRequest();
postRequest.BotName = "X";
postRequest.BotAlias = "X";
postRequest.UserId = Guid.NewGuid().ToString();
postRequest.InputText = "What Time Is My Appointment?";
try
{
response = await lexClient.PostTextAsync(postRequest);
context.Logger.Log(response.IntentName);
context.Logger.Log(response.DialogState);
}
catch (Exception ex)
{
context.Logger.Log($"EXCEPTION CAUGHT: {Environment.NewLine} {ex.ToJson()} {Environment.NewLine} {response.Message} {response.IntentName} {response.SlotToElicit}");
return "Error";
}
context.Logger.Log($"Success from lambda {Environment.NewLine} Message: {response.Message} {Environment.NewLine} " +
$"Dialog State:{response.DialogState}");
return "Success";
}
I am invoking this from a connect flow like so:
And what I'm getting back in return is:
"ErrorType": 2,
"ErrorCode": "DependencyFailedException",
"RequestId": "",
"StatusCode": 424,
"Message": "Invalid Lambda Response: Received error response from Lambda: Unhandled",
"Data": {},
"InnerException": {
"Response": {
"StatusCode": 424,
"IsSuccessStatusCode": false,
"ContentType": "application/json",
"ContentLength": 85,
"ResponseBody": {}
},
"Message": "Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown."
Which I read in the Amazon PostText Documentation could mean a couple things:
If Amazon Lex does not have sufficient permissions to call a Lambda
function.
If a Lambda function takes longer than 30 seconds to execute.
If a fulfillment Lambda function returns a Delegate dialog action
without removing any slot values.
I have confirmed that my Lambda does have permission to use PostText and access Lex. I have tried changing my return type of my function to PostTextReponse with no luck, So I'm not sure where to go from here, there isn't much documentation for this kind of thing.
Any help is appreciated, thanks!
For anybody that is curious about this I have found an answer:
First off, when using a Lambda function like this it is a good idea to return an object of what you want. Which is what I ended up doing. You also need to set SessionAttributes in JSON format.
My code is working and is as follows now:
public async Task<LambdaResponseItem> FunctionHandler(ConnectRequest request, ILambdaContext context)
{
var client = new AmazonLexClient();
var response = new PostContentResponse();
var lambdaInfo = new Dictionary<string, string>();
var contentRequest = new PostContentRequest();
var postContentStream = new MemoryStream();
var postContentWriter = new StreamWriter(postContentStream);
try
{
var userInput = request.Details?.Parameters?.GetValueOrDefault("UserInput");
postContentWriter.Write(userInput); // Grab user input (utterance) value from AWS Connect.
postContentWriter.Flush();
postContentStream.Position = 0;
contentRequest.Accept = "text/plain; charset=utf-8";
contentRequest.BotName = "IntroGreeting";
contentRequest.BotAlias = EnvironmentVariables.IsProduction ? "Production" : "Development";
contentRequest.ContentType = "text/plain; charset=utf-8";
contentRequest.UserId = request.Details?.ContactData?.ContactId;
contentRequest.InputStream = postContentStream;
contentRequest.SessionAttributes = request.Details?.Parameters?.ToJson(); // * Must be in Json format or request will return error *
// POST to Lex
response = await client.PostContentAsync(contentRequest);
return new LambdaResponseItem(){
Content = ""
}
}
catch (Exception ex)
{
context.Logger.Log($"POST Request to Amazon Lex Failed {ex.ToJson()}");
}
I have the following web server method, that returns data to our front-end applicaiton.
[FunctionName("SearchCustomerBySearchTerm")]
public static async Task<HttpResponseMessage> SearchCustomerBySearchTerm([HttpTrigger(AuthorizationLevel.Function, WebRequestMethods.Http.Get, Route = "Customer/SearchCustomerBySearchTerm/{searchTerm}/pageSize/{pageSize}")]HttpRequestMessage req, TraceWriter log, string searchTerm, int pageSize)
{
try
{
var continuationToken = req.Headers.TryGetValues("continuationToken", out IEnumerable<string> values) ? values.FirstOrDefault() : null;
PagedResponse<CustomerSearchResult> pagedResponse = await _customerComponent.FindCustomerBy(searchTerm, continuationToken, pageSize);
if (pagedResponse == null) return req.CreateResponse(HttpStatusCode.NoContent, $"Could not find any data related to {searchTerm}");
HttpResponseMessage responseMessage = req.CreateResponse(HttpStatusCode.OK, pagedResponse.Results);
responseMessage.Content.Headers.Add("continuationToken", pagedResponse.Continuation);
responseMessage.Content.Headers.Add("Access-Control-Expose-Headers", "*");
return responseMessage;
}
catch (Exception ex)
{
log.Error(ex.Message);
return req.CreateResponse(HttpStatusCode.InternalServerError, "Something went wrong. Could not search for customers");
}
}
I am allowing all headers to be exposed, by adding the Access-Control-Expose-Headers.
From my Angular application, I am doing the request as follow:
searchCustomersPaged(searchTerm: string, continuationToken: string): Observable<HttpResponse<CustomerSearchResult>> {
let customHeaders = new HttpHeaders().set("continuationToken", this.currentContinuationToken);
const url = "http://localhost:7071/api/Customer/SearchCustomerBySearchTerm/andrew/pageSize/10";
const parsedUrl = encodeURI(url);
return this.http.get<HttpResponse<CustomerSearchResult>>(parsedUrl, { headers: customHeaders });
}
As you can see above, I am expecting an HttpResponse<CustomerSearch> back.
Here is how I try and read my headers:
nextClikcedHandle(continuationToken: string): void {
this.customerService.searchCustomersPaged(this.customerService.searchTerm, this.currentContinuationToken)
.subscribe(resp => {
//add current continuation token, to previous now, as this will be used for 'previous' searching
this.previousContinuationTokens.push(this.currentContinuationToken);
//set next continuation token received by server
this.currentContinuationToken = resp.headers.get('continuationToken');
//return search results
this.customerService.searchResults.next(resp.body);
});
}
With the above code, the resp.headers and the resp.body is always undefined. Why is this happening?
If I look at the Network tab within Chrome, I can see my data is returned, as well as my header.
What am I doing wrong?
I found a useful article here:
By default the HttpClient returns the body of the response. You can
pass-in an object with an observe key set to a value of ‘response’ to
get the full response. This can be useful to inspect for certain
headers:
So I changed my code as follow, with the added observe key.
searchCustomersPaged(searchTerm: string, continuationToken: string): Observable<HttpResponse<CustomerSearchResult>> {
let customHeaders = new HttpHeaders().set("continuationToken", this.currentContinuationToken);
const url = "http://localhost:7071/api/Customer/SearchCustomerBySearchTerm/andrew/pageSize/10";
const parsedUrl = encodeURI(url);
return this.http.get<CustomerSearchResult>(parsedUrl, { headers: customHeaders, observe: 'response' });
}
After changing above method, I could query body and headers as per normal:
nextClikcedHandle(continuationToken: string): void {
this.customerService.searchCustomersPaged(this.customerService.searchTerm, this.currentContinuationToken)
.subscribe(resp => {
//add current continuation token, to previous now, as this will be used for 'previous' searching
this.previousContinuationTokens.push(this.currentContinuationToken);
//set next continuation token received by server
this.currentContinuationToken = resp.headers.get('continuationToken');
//return search results
this.customerService.searchResults.next(resp.body);
});
I have a MVC 5 backend written in C#. It serves MVC views written in Razor and also some Angular 2 pages.
What is the best way to handle potential errors when calling server from client? I really would like to establish a pattern that is robust and works in all situations. Below is what I have tried so far.
Backend C# code:
public class MyController : Controller
{
[HttpGet]
public ActionResult GetUsers()
{
try
{
// Lot of fancy server code ...
throw new Exception("Dummy error");
return GetCompressedResult(json);
}
catch (Exception ex)
{
throw new HttpException(501, ex.Message);
}
}
private FileContentResult GetCompressedResult(string json)
{
// Transform to byte array
var bytes = Encoding.UTF8.GetBytes(json);
// Compress array
var compressedBytes = bytes.Compress();
HttpContext.Response.AppendHeader("Content-encoding", "gzip");
return new FileContentResult(compressedBytes, "application/json");
}
}
Client side Angular 2 code:
public loadDataFromServer() {
let response = this.http.get(this.urlGetData)
.map((res: Response) => res.json())
.catch(this.handleError);
response.subscribe(response => {
// Process valid result ...
},
err => { console.error(err); }
);
};
private handleError(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = JSON.parse(JSON.stringify(error || null))
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
This is a printscreen of the error object processed by handleError method:
This all raises some questions:
Is it correct to throw custom HttpException from server?
Is handleError method correct or maybe too complex?
On client side I would like to see the custom error message, but currently it is just found in an enormous "blob" of HTML that is nasty to parse.
Is client side error handling necessary BOTH in get call and subscribe action?
My current suggestion is to let server respond with Json object for all handled exceptions.
On client side I check result object for possible error property before handling valid result.
The handleResponseError method will parse typed Response object and throw observable message. But at least my browser (Chrome 57) seems to automatically log response errors to console. So if subscriber need no specific extra handling for different errors, then the subscriber need no extra action for err object.
Please feedback if there are better ways!
Backend C# code:
public class MyController : Controller
{
[HttpGet]
public ActionResult GetUsers()
{
try
{
// Lot of fancy server code ...
throw new ArgumentException("Dummy error");
// Normal return of result ...
}
catch (Exception ex)
{
return Json(new { error = $"{ex.GetType().FullName}: '{ex.Message}'" }, JsonRequestBehavior.AllowGet);
}
}
}
Client side Angular 2 code:
public loadDataFromServer() {
let response = this.http.get(this.urlGetData)
.map((res: Response) => res.json())
.catch(this.handleResponseError);
response.subscribe(result => {
if (result.error) {
this.displayJsonError(this.urlGetUsers, result.error);
}
else {
// Process valid result
}
});
};
private handleResponseError(value: Response | any) {
let errorMessage = value.toString();
let response = value as Response;
if (response) {
errorMessage = `${response.status}: ${response.statusText}\n${response.toString()}`;
}
if (value.error) {
errorMessage = value.error;
}
if (value.message) {
errorMessage = value.message;
}
return Observable.throw(errorMessage);
}
private displayJsonError(url: string, error: string) {
console.error(`Call to '${url}' failed with ${error}`);
}
So I am looking for a pattern on how to handle exceptions. Specifically I want to be able to pass the exception message on to the client from a Web API controller.
The client is using a third party library which deals with a call to the API
as
this.msgs = [];
let xhr = new XMLHttpRequest(),
formData = new FormData();
for(let i = 0; i < this.files.length; i++) {
formData.append(this.name, this.files[i], this.files[i].name);
}
xhr.upload.addEventListener('progress', (e: ProgressEvent) => {
if(e.lengthComputable) {
this.progress = Math.round((e.loaded * 100) / e.total);
}
}, false);
xhr.onreadystatechange = () => {
if(xhr.readyState == 4) {
this.progress = 0;
if(xhr.status == 200)
this.onUpload.emit({xhr: xhr, files: this.files});
else
this.onError.emit({xhr: xhr, files: this.files});
this.clear();
}
};
xhr.open('POST', this.url, true);
xhr.send(formData);
My current call back function is such
errorComplete(event: any) {
console.log("upload error");
}
notice that on error the library just wraps up the XMLHttpRequest and passes it on to my call back function.
so in the controller I have created a test line as follows
throw new Exception("This is a test message");
This is to simulate an unexpected exception
currently the return code in the XMLHttpRequest is a 500 and the text is the html that .Net generates when an exception occurs.
yes the method in my controller will need to wrapper in a try-catch but I am not sure of what code to put in the catch so I can send the error message down to the client and it can handle it and not take down the application.
the current use case I am looking at is the user uploads a file to the system but there is already a file with the specified name in the system. And renaming the file is not an option! I need to notify the user that there is already a file with that name in the system.
google has not revealed a way to pass the message back so I can process it.
Thank you Nkosi- your comment got me on the right track.
I implemented some middleware.
public class UIExceptionHandler
{
RequestDelegate _next;
public UIExceptionHandler(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await this._next(context);
}
catch (Exception x)
{
if (!context.Response.HasStarted)
{
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
context.Response.Headers["Message"] = x.Message;
}
}
}
}
public static class UIExcetionHandlerExtensions
{
public static IApplicationBuilder UseUIExceptionHandler(this IApplicationBuilder builder)
{
return builder.UseMiddleware<UIExceptionHandler>();
}
}
and in the configure method of the startup
app.UseUIExceptionHandler();
then on the client I can do
errorComplete(event: any) {
var errorMessage = event.xhr.getResponseHeader('Message');
console.log(errorMessage);
}
If anyone sees an issue with this solution please let me know
public IHttpActionResult Save(item)
{
try
{
result = MyRepository.Save(item);
return Ok(result);
}
catch
{
// What should I do here?
// I wish to return an error response how can i do that?
}
}
If it has no exception,I can return Ok.
But if there is an exception what should I do?
Note that I have javascript client
In Web API 2, you can use BadRequest to return an error message.
public IHttpActionResult Save(item)
{
try
{
result = MyRepository.Save(item);
return Ok(result);
}
catch
{
return BadRequest("Error message");
}
}
FYI: Do not use try catch block just to swallow an exception. I personally do not like using try catch block inside Action method. Instead, I let Web API 2 Global Exception Handler handles exceptions. It is the out of the scope of your original question.
Create a model to hold what error information you want to return and pass that.
public IHttpActionResult Save(item) {
try {
result = MyRepository.Save(item);
return Ok(result);
} catch {
// What should I do here? Create a model to hold the data you want to return
var myErrorModel = new {
code = "My custom error code or 500 server error code",
message = "some friendly error message"
};
// I wish to return an error response how can i do that?
var response = Request.CreateResponse(HttpStatusCode.InternalServerError, myErrorModel);
return ResponseMessage(response);
}
}
In your javascript client in the error handler you can then access the model properties and handle the error as you see fit.
var handleResponse = function (data) {
var code = data.code;
var message = data.message
};
UPDATE:
Agreeing with #Win, I personally don't like doing this in the controller actions as it is a Cross-cutting concern and have basically moved everything from within the catch block into a global error handler.
You can use HttpResponseException to send an error message.
public IHttpActionResult Save(item)
{
try
{
result = MyRepository.Save(item);
return Ok(result);
}
catch
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("Error Message!!"),
ReasonPhrase = "Error Reason phrase"
};
throw new HttpResponseException(resp);
}
}
Use HttpResponseException as below:
throw new HttpResponseException(HttpStatusCode.InternalServerError)
Or another way is:
var response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent("some error"),
ReasonPhrase = "Not Found"
};
throw new HttpResponseException(response);