Failsafe exception should render JSON if request.format == :json #5660

Closed
southpolesteve opened this Issue Mar 30, 2012 · 2 comments

Comments

Projects
None yet
5 participants
Contributor

southpolesteve commented Mar 30, 2012

Occasionally I have run into a case where an exception occurs and the failsafe exception template is rendered

Relavent code is here: https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_dispatch/middleware/show_exceptions.rb

Could this be written to return JSON if the request format is JSON?

I run into issues with this when using JSON rails apis. The other server is usually expecting a JSON body, and often chokes on an HTML formatted response.

I am going to dig into this a bit, but most of this code is beyond my understanding at the moment. Would love to get input from anyone who knows more about Rails exception rendering.

@wycats what do you think of this? Is there any sense for something like this in development mode? I don't think there is any real point to doing it.

Member

wycats commented Apr 29, 2012

For me, I'd rather see the results in plaintext than JSON, because I am usually looking at the results via curl or the Chrome inspector, which shows the error even though the client choked.

Showing errors to a machine client (JSON parser) as HTML is pretty silly and it's annoying to debug. Can someone open a pull request with this?

wycats closed this Apr 29, 2012

rafaelfranca reopened this Apr 29, 2012

@jeyb jeyb added a commit to jeyb/rails that referenced this issue Jul 6, 2012

@jeyb jeyb Fixes issue #5660: Failsafe exception returns text/html and text/plain.
If the request format is text/html then return the existing failsafe
response. For all other formats return text/plain.

The original issue points out to return JSON at the appropriate time,
however as @wycats pointed out, it's often better to return text/plain.
7fa32fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment