Created
December 9, 2014 16:29
-
-
Save brenes/7b0c41a4408ccb960163 to your computer and use it in GitHub Desktop.
Rack error redirector
This file contains hidden or 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
# This rack engine redirects to external URLs when some error is thrown. | |
# How to use it? | |
# Just add the rack middleware in config/application.rb | |
# require 'rack/error_redirection' | |
# config.app_middleware.insert_before('ActionDispatch::ShowExceptions', 'Rack::ErrorRedirection') | |
# And fill the error_404_url and error500_url variables | |
module Rack | |
class ErrorRedirection | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
response = @app.call(env) | |
Rails.logger.debug response.inspect | |
if response[0] == "404" | |
["302", { Location: error_404_url}, ''] | |
elsif response[0] == "500" | |
["302", { Location: error_500_url}, ''] | |
else | |
response | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment