2017-06-30 01:10:34 -04:00
|
|
|
require_relative "../http/request"
|
|
|
|
require_relative "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
|
2017-03-12 12:51:26 -04:00
|
|
|
# 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
|
2016-08-06 12:51:43 -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)
|
2015-08-06 19:12:06 -04:00
|
|
|
request = ActionDispatch::Request.new env
|
2013-01-07 07:58:55 -05:00
|
|
|
@app.call(env)
|
|
|
|
rescue Exception => exception
|
2015-08-06 19:12:06 -04:00
|
|
|
if request.show_exceptions?
|
2015-08-23 20:25:13 -04:00
|
|
|
render_exception(request, exception)
|
2015-08-06 19:12:06 -04:00
|
|
|
else
|
|
|
|
raise exception
|
2013-10-26 13:55:22 -04:00
|
|
|
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
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_exception(request, exception)
|
|
|
|
backtrace_cleaner = request.get_header "action_dispatch.backtrace_cleaner"
|
|
|
|
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception)
|
|
|
|
status = wrapper.status_code
|
|
|
|
request.set_header "action_dispatch.exception", wrapper.exception
|
|
|
|
request.set_header "action_dispatch.original_path", request.path_info
|
|
|
|
request.path_info = "/#{status}"
|
|
|
|
response = @exceptions_app.call(request.env)
|
|
|
|
response[1]["X-Cascade"] == "pass" ? pass_response(status) : response
|
|
|
|
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
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def pass_response(status)
|
2016-08-16 03:30:11 -04:00
|
|
|
[status, { "Content-Type" => "text/html; charset=#{Response.default_charset}", "Content-Length" => "0" }, []]
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
end
|