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}`);
}
Related
I am trying to invoke External APIs from AWS lambda function written in c#. The Lamda function is deployed in No VPC mode. I am calling this function from Alexa skill. The code works fine for an http request, but its not working for https.
The below code works when I use http://www.google.com.
But, if I replace http with https, then I get the error in the cloud watch saying:
"Process exited before completing request."
Even the log written in catch is not getting logged in cloud watch.
public class Function
{
public const string INVOCATION_NAME = "bingo";
public async Task<SkillResponse> FunctionHandler(SkillRequest input, ILambdaContext context)
{
var requestType = input.GetRequestType();
if (requestType == typeof(IntentRequest))
{
string response = "";
IntentRequest request = input.Request as IntentRequest;
response += $"About {request.Intent.Slots["carmodel"].Value}";
try
{
using (var httpClient = new HttpClient())
{
Console.WriteLine("Trying to access internet");
//var resp=httpClient.GetAsync("http://www.google.com").Result // this works perfect!
var resp = httpClient.GetAsync("https://www.google.com").Result; // this throws error
Console.WriteLine("Call was successful");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception from main function " + ex.Message);
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine(ex.StackTrace);
}
return MakeSkillResponse(response, true);
}
else
{
return MakeSkillResponse(
$"I don't know how to handle this intent. Please say something like Alexa, ask {INVOCATION_NAME} about Tesla.",
true);
}
}
private SkillResponse MakeSkillResponse(string outputSpeech, bool shouldEndSession,
string repromptText = "Just say, tell me about car models to learn more. To exit, say, exit.")
{
var response = new ResponseBody
{
ShouldEndSession = shouldEndSession,
OutputSpeech = new PlainTextOutputSpeech { Text = outputSpeech }
};
if (repromptText != null)
{
response.Reprompt = new Reprompt() { OutputSpeech = new PlainTextOutputSpeech() { Text = repromptText } };
}
var skillResponse = new SkillResponse
{
Response = response,
Version = "1.0"
};
return skillResponse;
}
}
The issue was resolved by updating the library version.
System.Net.Http v4.3.4 was not completely compatible with dotnet core v1.
So outbound http calls were working but not https calls. Changing the version of System.net.http resolved the issue.
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);
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());
I have a webservice written in C#. When errors occur on the server, I would like to inform the user client side. This works fine by throwing exceptions server side, which is then sent to my error handler client side.
However, I would like to, when I throw the exception, to set a property describing how serious I think the error is at this point. Thus I can decide client side how to display the error:
WebService.Method(some_value, handle_response, handle_error);
function handle_response (response) {
//Do something...
}
function handle_error (error) {
if(error.level === 'Critical') {
//Show critical message.
} else if(error.level === 'Warning') {
//Show warning message.
} else
...
}
}
My solution so far has been to create a custom exception inheriting from System.Exception.
My webservice returns with a JSON formatted result.
My problem is how to get my property through to the client side JSON response?
Web service:
public Response zzz() {
Response result;
try {
...
} catch (MyException) {
result.HasError = true;
result.Error.Level = Normal;
result.Error.Message = "It's OK.";
} catch (Exception) {
result.HasError = true;
result.Error.Level = Critical;
result.Error.Message = "!!!!";
}
}
Then check Response.HasError