How to Rescue from ActionDispatch::ParamsParser::ParseError in Rails 4
Turns out that further up the middleware stack, ActionDispatch::ShowExceptions can be configured with an exceptions app.
module Traphos class Application < Rails::Application # For the exceptions app require "#{config.root}/lib/exceptions/public_exceptions" config.exceptions_app = Traphos::PublicExceptions.new(Rails.public_path) endend
Based heavily on the Rails provided one I am now using:
module Traphos class PublicExceptions attr_accessor :public_path def initialize(public_path) @public_path = public_path end def call(env) exception = env["action_dispatch.exception"] status = code_from_exception(env["PATH_INFO"][1..-1], exception) request = ActionDispatch::Request.new(env) content_type = request.formats.first body = {:status => { :code => status, :exception => exception.class.name, :message => exception.message }} render(status, content_type, body) end private def render(status, content_type, body) format = content_type && "to_#{content_type.to_sym}" if format && body.respond_to?(format) render_format(status, content_type, body.public_send(format)) else render_html(status) end end def render_format(status, content_type, body) [status, {'Content-Type' => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]] end def render_html(status) found = false path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale path = "#{public_path}/#{status}.html" unless path && (found = File.exist?(path)) if found || File.exist?(path) render_format(status, 'text/html', File.read(path)) else [404, { "X-Cascade" => "pass" }, []] end end def code_from_exception(status, exception) case exception when ActionDispatch::ParamsParser::ParseError "422" else status end end endend
To use it in a test environment requires setting config variables (otherwise you get the standard exception handling in development and test). So to test I have (edited to just have the key parts):
describe Photo, :type => :api do context 'update' do it 'attributes with non-parseable json' do Rails.application.config.consider_all_requests_local = false Rails.application.config.action_dispatch.show_exceptions = true patch update_url, {:description => description} response.status.should eql(422) result = JSON.parse(response.body) result['status']['exception'].should match(/ParseError/) Rails.application.config.consider_all_requests_local = true Rails.application.config.action_dispatch.show_exceptions = false end endend
Which performs as I need in a public API way and is adaptable for any other exceptions I may choose to customise.
This article (also from 2013) thoughtbot covers also this topic. They put their response inside this middleware service only if you requested json
if env['HTTP_ACCEPT'] =~ /application\/json/ error_output = "There was a problem in the JSON you submitted: #{error}" return [ 400, { "Content-Type" => "application/json" }, [ { status: 400, error: error_output }.to_json ] ]else raise errorend