2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-12-16 03:29:37 -05:00
|
|
|
module ActionDispatch
|
2014-08-08 17:25:18 -04:00
|
|
|
# When called, this middleware renders an error page. By default if an HTML
|
2017-08-26 02:30:39 -04:00
|
|
|
# response is expected it will render static error pages from the <tt>/public</tt>
|
2014-08-05 12:45:44 -04:00
|
|
|
# directory. For example when this middleware receives a 500 response it will
|
2017-08-26 02:30:39 -04:00
|
|
|
# render the template found in <tt>/public/500.html</tt>.
|
2014-08-05 12:45:44 -04:00
|
|
|
# If an internationalized locale is set, this middleware will attempt to render
|
2017-08-26 02:30:39 -04:00
|
|
|
# the template in <tt>/public/500.<locale>.html</tt>. If an internationalized template
|
|
|
|
# is not found it will fall back on <tt>/public/500.html</tt>.
|
2014-08-05 12:45:44 -04:00
|
|
|
#
|
|
|
|
# When a request with a content type other than HTML is made, this middleware
|
|
|
|
# will attempt to convert error information into the appropriate response type.
|
2011-12-16 03:29:37 -05:00
|
|
|
class PublicExceptions
|
|
|
|
attr_accessor :public_path
|
|
|
|
|
2012-06-12 12:24:56 -04:00
|
|
|
def initialize(public_path)
|
2011-12-16 03:29:37 -05:00
|
|
|
@public_path = public_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2012-06-11 15:58:24 -04:00
|
|
|
request = ActionDispatch::Request.new(env)
|
2015-08-23 20:22:21 -04:00
|
|
|
status = request.path_info[1..-1].to_i
|
2019-03-25 16:51:22 -04:00
|
|
|
begin
|
|
|
|
content_type = request.formats.first
|
2020-10-07 10:48:54 -04:00
|
|
|
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
|
2019-03-25 16:51:22 -04:00
|
|
|
content_type = Mime[:text]
|
|
|
|
end
|
|
|
|
body = { status: status, error: Rack::Utils::HTTP_STATUS_CODES.fetch(status, Rack::Utils::HTTP_STATUS_CODES[500]) }
|
2012-06-11 15:58:24 -04:00
|
|
|
|
2012-06-14 20:58:05 -04:00
|
|
|
render(status, content_type, body)
|
2012-06-11 15:58:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def render(status, content_type, body)
|
|
|
|
format = "to_#{content_type.to_sym}" if content_type
|
|
|
|
if format && body.respond_to?(format)
|
|
|
|
render_format(status, content_type, body.public_send(format))
|
|
|
|
else
|
|
|
|
render_html(status)
|
|
|
|
end
|
2011-12-16 03:29:37 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_format(status, content_type, body)
|
2016-08-16 03:30:11 -04:00
|
|
|
[status, { "Content-Type" => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
|
|
|
|
"Content-Length" => body.bytesize.to_s }, [body]]
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2012-06-11 15:58:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_html(status)
|
|
|
|
path = "#{public_path}/#{status}.#{I18n.locale}.html"
|
|
|
|
path = "#{public_path}/#{status}.html" unless (found = File.exist?(path))
|
2011-12-16 03:29:37 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
if found || File.exist?(path)
|
|
|
|
render_format(status, "text/html", File.read(path))
|
|
|
|
else
|
|
|
|
[404, { "X-Cascade" => "pass" }, []]
|
|
|
|
end
|
2012-06-11 15:58:24 -04:00
|
|
|
end
|
2011-12-16 03:29:37 -05:00
|
|
|
end
|
2012-06-11 15:58:24 -04:00
|
|
|
end
|