This blog is part of our Rails 5 series.
Rails 4.x returns error information in HTML page whenever there is any exception, in the development environment.
This is fine for normal HTML requests. But traditionally, Rails always returned with HTML response for exceptions for all requests, including JSON on XML requests in development.
We can now generate API only apps in Rails 5.
In case of such apps,
to have the error message
in the format in which request was made.
Having an HTML response for a JSON endpoint
is not going to help in debugging why the exception happened.
New config option debug_exception_response_format
Rails 5 has introduced new configuration to respond with proper format for exceptions.
Let’s see an example of the response received with this configuration.
status key will represent HTTP status code
error key will represent the corresponding Rack HTTP status.
exception will print the output of actual exception in
traces will contain application and framework traces similar to how they are displayed in
HTML error page.
config.debug_exception_response_format is set to
so as to render responses in the same format as requests.
If you want the original behavior of rendering HTML pages, you can configure this option as follows.