Skip to content

Instantly share code, notes, and snippets.

@elarkin
Last active December 12, 2015 03:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elarkin/4709748 to your computer and use it in GitHub Desktop.
Save elarkin/4709748 to your computer and use it in GitHub Desktop.
Handling Parsing errors using Rack middleware in Rails 3.2
#in application.rb
module YourApp
class Application < Rails::Application
config.middleware.insert_before ActionDispatch::ParamsParser, "ParsingFailureToJSON"
...
end
end
#The Parsing failure middleware
class ParsingFailureToJSON
def initialize app
@app = app
end
def call env
begin
@app.call(env)
rescue DecodeError #Obviously, this should be the error that you are interested in
RailsFailureController.action(:parsing).call(env)
end
end
end
#RailsFailureController is a controller that can render anything using the full rails view layer.
#It makes rendering parsing errors using reusable error templates easy. You don't have to use a controller though
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment