2009-12-22 18:11:21 -05:00
|
|
|
require 'action_dispatch/http/request'
|
2011-12-01 14:02:00 -05:00
|
|
|
require 'action_dispatch/middleware/exception_wrapper'
|
2009-06-08 16:33:18 -04:00
|
|
|
|
2009-05-03 00:02:22 -04:00
|
|
|
module ActionDispatch
|
2011-12-01 15:15:42 -05:00
|
|
|
# This middleware rescues any exception returned by the application
|
2011-12-16 03:59:36 -05:00
|
|
|
# and calls an exceptions app that will wrap it in a format for the end user.
|
2011-12-16 03:45:14 -05:00
|
|
|
#
|
2011-12-16 03:59:36 -05:00
|
|
|
# The exceptions app should be passed as parameter on initialization
|
2012-09-28 16:32:27 -04:00
|
|
|
# of ShowExceptions. Every time there is an exception, ShowExceptions will
|
2011-12-16 03:45:14 -05:00
|
|
|
# store the exception in env["action_dispatch.exception"], rewrite the
|
|
|
|
# PATH_INFO to the exception status code and call the rack app.
|
2012-03-16 22:23:00 -04:00
|
|
|
#
|
2011-12-16 03:59:36 -05:00
|
|
|
# If the application returns a "X-Cascade" pass response, this middleware
|
|
|
|
# will send an empty response as result with the correct status code.
|
|
|
|
# If any exception happens inside the exceptions app, this middleware
|
|
|
|
# catches the exceptions and returns a FAILSAFE_RESPONSE.
|
2009-05-03 00:02:22 -04:00
|
|
|
class ShowExceptions
|
2012-07-06 01:39:15 -04:00
|
|
|
FAILSAFE_RESPONSE = [500, { 'Content-Type' => 'text/plain' },
|
2013-01-06 17:36:11 -05:00
|
|
|
["500 Internal Server Error\n" \
|
|
|
|
"If you are the administrator of this website, then please read this web " \
|
|
|
|
"application's log file and/or the web server's log file to find out what " \
|
2012-07-06 01:39:15 -04:00
|
|
|
"went wrong."]]
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2011-12-20 09:12:38 -05:00
|
|
|
def initialize(app, exceptions_app)
|
2009-05-03 00:02:22 -04:00
|
|
|
@app = app
|
2011-12-16 03:29:37 -05:00
|
|
|
@exceptions_app = exceptions_app
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2013-01-07 07:58:55 -05:00
|
|
|
@app.call(env)
|
|
|
|
rescue Exception => exception
|
2013-10-26 13:55:22 -04:00
|
|
|
if env['action_dispatch.show_exceptions'] == false
|
|
|
|
raise exception
|
|
|
|
else
|
|
|
|
render_exception(env, exception)
|
|
|
|
end
|
2009-05-17 13:24:42 -04:00
|
|
|
end
|
2009-05-11 20:07:05 -04:00
|
|
|
|
2009-05-17 13:24:42 -04:00
|
|
|
private
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2011-12-16 03:29:37 -05:00
|
|
|
def render_exception(env, exception)
|
|
|
|
wrapper = ExceptionWrapper.new(env, exception)
|
2011-12-16 03:59:36 -05:00
|
|
|
status = wrapper.status_code
|
2011-12-16 03:29:37 -05:00
|
|
|
env["action_dispatch.exception"] = wrapper.exception
|
2011-12-16 03:59:36 -05:00
|
|
|
env["PATH_INFO"] = "/#{status}"
|
|
|
|
response = @exceptions_app.call(env)
|
|
|
|
response[1]['X-Cascade'] == 'pass' ? pass_response(status) : response
|
2011-12-01 15:15:42 -05:00
|
|
|
rescue Exception => failsafe_error
|
|
|
|
$stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}"
|
|
|
|
FAILSAFE_RESPONSE
|
|
|
|
end
|
2011-12-16 03:59:36 -05:00
|
|
|
|
|
|
|
def pass_response(status)
|
|
|
|
[status, {"Content-Type" => "text/html; charset=#{Response.default_charset}", "Content-Length" => "0"}, []]
|
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
end
|