Closed tigris closed 10 years ago
You should be able to override the response in your resources' #finish_request
method.
The FSM specs illustrate the behaviour of error handling, #handle_exception
, and #finish_request
: spec/webmachine/decision/fsm_spec.rb
If you know resource_exists?
is going to return something falsey, you can also pre-emptively set the content-type header and body like so:
response.headers['Content-Type'] = 'application/json'
response.body = '{"error":"not_found"}'
Perfect, thanks @seancribbs, that was much easier than my error handling / raising idea. Feel free to close this.
hey @seancribbs - as had come up in #131 - could it now be useful to be able to define a custom resource to handle the case of a 404 for a resource who could not be routed to? I'm still waiting on details from @Asmod4n but it may be generally useful for cases like this too.
@robgleeson Code something up and let's see. My gut feeling is that should not be a resource itself necessarily, but a canned response/method, maybe attached to the Application.
Made a repro in #144, would be nice if one could define custom error resources like handle_exception but as a resource you could add to Webmachine.application
I'm a bit conflicted here too.
at one stage I saw NotFoundResource
replacing what Webmachine.render_error(404, …)
does by being responsible for drawing the 404 response body. NotFoundResource
could be replaced by your own resource who would of course could draw its own response and implement compression if it wanted to be. however, it's a bit strange, because #finish_request
in the Resource who forced the 404 is never really respected.
infact it'd be super strange and not fit into anything. it suffers from infinite recursion as an idea by itself now.
the only possible way to make it work is to treat a '404 resource' as a special case (maybe defined not_found_resource?
) who could short-circuit going back through the FSM.
Currently if false/nil is returned from
resource_exists?
the response will betext/html
regardless of the content-types you have implemented in your resource.We are dealing purely in json, it seems easy enough to return a json based error response with custom error codes via the
handle_exception
method. However, it would seem logical to me if there was a way to set the json response body of a 404?Alternatively we can raise some kind of custom NotFound error message from the
resource_exists?
callback, and then handle that error manually. Seems clunky, but would work.