Quantcast

Using Exceptions to Map HTTP Response Codes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Using Exceptions to Map HTTP Response Codes

paulh
This post has NOT been accepted by the mailing list yet.
Within the context of a JSON API, as opposed to gsp pages, I'm trying to simplify some of the error handling.  I have the following maps:

"404" (controller: "error", action: "notFound", exception: NotFoundException)
"500" (controller: "error", action: "general")

When I throw a NotFoundException from within my code I wind up in ErrorController.general instead of ErrorController.notFound.  I'm assuming that it is legal to declare a 404 error handler with an exception but that it is ignored and any exceptions that percolate to the above mappings will always be handled by ErrorController.general.  I also tried:

"500" (controller: "error", action: "notFound", exception: NotFoundException)
"500" (controller: "error", action: "general")

And then setting the status code to 404 within the ErrorController.notFound method.  However, it reverts back to a 500.  While there's some debate as to whether 404 is the *best* exception, we would like to apply this logic to 400 and 502 errors as well.  For example:

"502" (controller: "error", action: "externalService", exception: ExternalServiceProviderNoResponse)
"400" (controller: "error", action: "argumentError", exception: InvalidArgumentError)

Is there a work around so that we can centralize and simplify exception handling?
Loading...