Last active
December 12, 2015 03:48
-
-
Save elarkin/4709748 to your computer and use it in GitHub Desktop.
Handling Parsing errors using Rack middleware in Rails 3.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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