lib/rambulance/exceptions_app.rb in rambulance-0.5.0 vs lib/rambulance/exceptions_app.rb in rambulance-0.6.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
module Rambulance
ERROR_HTTP_STATUSES = Rack::Utils::SYMBOL_TO_STATUS_CODE.select do |status_in_words, http_status|
# Exclude http statuses that:
# * represent a successful status(2xx, 3xx)
# * are unassigned(427, 430, 509)
@@ -32,22 +34,21 @@
def self.local_prefixes
[Rambulance.view_path]
end if ActionPack::VERSION::STRING >= "4.2.0"
ERROR_HTTP_STATUSES.values.each do |status_in_words|
- eval <<-ACTION
+ eval <<-ACTION, nil, __FILE__, __LINE__ + 1
def #{status_in_words}
render(template_exists?(error_path) ? error_path : error_path(:internal_server_error))
end
ACTION
end
def process(action, *args)
if action.to_s.empty?
- action = request.env["PATH_INFO"][1..-1].to_sym.tap do |status_in_words|
- request.env["PATH_INFO"] = "/#{Rack::Utils::SYMBOL_TO_STATUS_CODE[status_in_words]}"
- end
+ action = request.env["PATH_INFO"][1..-1].to_sym
+ request.env["PATH_INFO"] = "/#{Rack::Utils::SYMBOL_TO_STATUS_CODE[action]}"
end
super
end
@@ -62,9 +63,17 @@
begin
request.POST
rescue BAD_REQUEST_EXCEPTION, ArgumentError
request.env["MALFORMED_BODY"], request.env["rack.input"] = request.env["rack.input"], StringIO.new
+ end
+
+ # The #formats method needs to be called after the sanitization above.
+ if request.respond_to?(:formats)
+ request.formats << Mime::Type.lookup('text/plain')
+ elsif request.respond_to?(:format) && status == 406
+ # TODO: Remove this conditional when dropping support for Rails 3.2
+ request.format = Mime::Type.lookup('text/plain')
end
super
end