Stack: Ruby 2.2, Rails 4.2, and Honeybadger.
I've been working on the error handling code for my site. I've noticed that error handling has become a real mess though out the site, and I've been looking for a way to standardize error handling.
The following "generic" error types have been found:
Record/resource not found
Failure to save record
Validation issues
Rights/Permissions errors
Other (Usually probing bots, double clicks, or WTF?)
With the following resulting actions.
Notify Error Collating resource (maybe)
Save data; if a particularly long form input
Response: Redirect to an error page (flash[:error]='?')
Response: Return a JSON string (AJAX requests)
Response: Various Status codes http 3xx, 4xx, 5xx
Customized message for developer (maybe); quite lengthy in some cases
Customized message for user
Silently do nothing
And then process issues
Roll back
return (exiting the action; and not allowing further action)
Does anyone know of a Gem/Module/Tutorial that handles these collective issues holistically?
Thank you in advance!
-daniel
I think the best answer, is to throw an exception
Controller code is responsible for getting the parameters, sanitizing them, role/safety checks, making calls to GET data (this should be business logic), and the preparing the data for export back to the browser.
I'll add on to this handling exceptions from the code.
Then business logic, should throw an error should something fatalistic happen.
Related
I am adding code to my iOS app to deal with errors when it interact with a server. The error handling varies based on type of failure.
For e.g: The error could be receipt of a non 200:OK response. In which case, I want to use the response code to determine the retry algorithm
Or the error could be a timeout
Or the error could be based on the value of a field in the JSON body returned from the server.
Also, the error handling would be different depending on whether the failure occurred during registration or during data transfer etc.
I am thinking of having a generic function to avoid repeating code in several places
Basically it will take two arguments
1. A failure code that identifies sub-type of error and
2. A JSON body whose content will depend on the failure code argument
Is this approach ok or is it better split it into individual functions?
The approach with one function is OK, but as you said, sometimes you will have message as body from JSON response, sometimes you will have error as timeout, sometimes as key from the 200 OK response.. It means you need a parser function which will decide what to pass as the error message, because I'm afraid that you will end up with huge function doing everything at once. I would:
a) Split this function to parser & error show
or
b) Do the parsing in the network handler, but it might copy the code across the application.
I am very new to web api stuff:
I am getting an error
406: Not Acceptable
error message in asp.net web api rest service.
In my rest service I’m using media format for my customized XML output, to get customized output.
I’m registering my formatted media in Global.asax page.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new mynewformat());
all my methods are post methods with typed object as parameter and parameters are accepts from body.
Whenever I try to test the service… Getting 406: Not acceptable error message.
can anyone please help me ... what could be the reason for this....???
I did notice couple of interesting points here...
If I’m commenting below line then I’m getting 200 (OK) status code (which is fine.)... but format is not applying to output.
GlobalConfiguration.Configuration.Formatters.Clear();
If i'm removing parameters in my service method.. Then its working
fine..
I request everyone.. Please guide me what could be the reason/work around/solution/fix..for this issue.
Note:I don't want accept parameters from URI so i made it to accept from frombody only.
Thanks.
There is a lot more to implementing a custom format than just adding it to the configuration formatters. It starts with having to change the media-type header to a new custom type of your choosing (like "application/myNewFormat") for all requests, for the client. On the back end, you have to implement a new MediaTypeFormatter that can handle the serialization. This involves a bit more of code.
A good example of this resides here, it can easily be stripped to boiler-plate code:
http://www.codeproject.com/Articles/559378/Implementing-Custom-Media-Formatters-in-ASP-NET-We
I notice OpenRasta.Core has an HtmlErrorCodec which is responsible for rendering a the server error page sent out when a handler throws an Exception.
When I make an JSON Ajax request to an exception throwing handler this Codec is selected and the exception is rendered as HTML.
I have tried to register my own IMediaTypeWriter for IList<Error> with MediaType("application/json") so I can send back JSON to the browser, but it seems to be ignored. Can anyone help?
Thanks
Neil
If there is an error, indeed a codec with IList will be selected, but will follow the normal conneg for a type.
I'd suggest having a look at the request log and finding out how and why the html codec gets selected (I'd suspect with my remote debugging tunnel vision that you may have a browser sending the equivalent of Accept: text/html,application/json, at which point OR doesn't really know which of the two is acceptable, which is probably a bug as we register text/html with a q of 1 where it should be 0.5). If that's indeed what the problem is, the solution is to remove the registration for the html error codec, which you can do by providing your own DependencyRegistrar.
Can you just catch your exceptions, wrap them in a type and do something like:
ResourceSpace.Has.ResourcesOfType<MyErrorWrapper>().WithoutUri.AsJsonDataContract()
We're using MVC .NET and the RouteCollection class to route URLs in our web app. This functions normally until we pass a URL containing the the text "PRN" anywhere inside the URL. When this happens, the routing will not occur and a 400 Page Not Found error is returned to the client. It's like something is throwing the error before the routing collection is even consulted, because the route the URL should take is never touched (by that I mean the underlying code's break-point is never hit, though the exact same URL without the string "PRN" will hit the break-point).
So I thought it might be a page validation issue, that maybe Microsoft decided to throw exceptions when the URL contains the phrase "PRN" because it is like "print" or "porn" but if that were the case then we'd see the "A potentially dangerous Request.Form value was detected from the client" error, but we don't.
Researching this has been a hassle because Google thinks PRN should return results for "porn", which means 98% of my search results are invalid (and inappropriate). Using the "-porn" clause in Google drops your results down to about 10-30 hits, all useless.
Does anyone know why a URL containing the string "PRN" will not route properly? If you have any posts or threads to point me to, that would be awesome (again, Google has failed me).
I've got a bunch of XHR actions in a controller, which return some HTML to insert into the page. If the response is an error, then it puts the output into a special error div. So far, nothing particularly interesting.
However, this general process doesn't work for Rails' exception handling. If I raise an exception in my XHR actions, I get the generic 500 error handler output in my error div, which looks a bit horrific. While I can catch all possible exceptions in my action and render a more appropriate error, I lose the standard exception logging and notification, which sucks.
So, the only solution I can think of is being able to specify a different 500 handler HTML fragment to use for these specific actions, but I'm not finding much. Anyone got any ideas?
You should be able to check for the 500 status code in your javascript handler and display a generic message like "Server Problem". If there are cases where a more specific error message would be useful to an end user in a production environment, you'll have to catch those exceptions with a rescue_from clause. If you really want to prevent the 500 page from showing, you can override the rescue_action_in_public method on your XHR controller.