Skip to content

Instantly share code, notes, and snippets.

@brenes
Created December 9, 2014 16:29
Show Gist options
  • Save brenes/7b0c41a4408ccb960163 to your computer and use it in GitHub Desktop.
Save brenes/7b0c41a4408ccb960163 to your computer and use it in GitHub Desktop.
Rack error redirector
# 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