Skip to content

Commit

Permalink
Force text error template when exceptions occur
Browse files Browse the repository at this point in the history
Rails by default shows nice error pages, with a lot of markup, css and
javascript. This is cool when you visit the page in the browser, but
not helpful when you consume the app as an API. Rails' HTML response
then makes it really hard find the error when it's displayed as part of
the exception message.

But Rails will render a text-based exception when doing AJAX requests.
By setting the `X-Requested-With` header we can make Rails show the
text template for all requests.

Note that there's still some markup in the error. This is a Rails bug,
addressed in rails/rails#22172.

I feel this is not a great solution. I'm planning on making a PR on
Rails to show the text-template for all non-HTML requests.
  • Loading branch information
tijmenb committed Nov 5, 2015
1 parent 59d02fe commit d51d04a
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -6,6 +6,11 @@ class BadRequest < StandardError; end
head :bad_request
end

before_action do
# Force Rails to show text-error pages
request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
end

private
def respond_with_command_error(error)
render status: error.code, json: error
Expand Down

0 comments on commit d51d04a

Please sign in to comment.