1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_dispatch/middleware/actionable_exceptions.rb
Ryuta Kamizono 92ff708476 Re-enable Layout/SpaceAroundOperators cop
We prefer space around operators, but `Layout/SpaceAroundOperators` cop
was temporarily disabled in #36943 since that cop changed to check
alignment strictly somehow.

In RuboCop 1.0.0, that is fixed by https://github.com/rubocop-hq/rubocop/pull/8906.

Related https://github.com/rails/rails/pull/38034#discussion_r359845661,
https://github.com/rails/rails/pull/39770#discussion_r448829561.
2020-10-23 16:12:15 +09:00

46 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require "erb"
require "uri"
require "action_dispatch/http/request"
require "active_support/actionable_error"
module ActionDispatch
class ActionableExceptions # :nodoc:
cattr_accessor :endpoint, default: "/rails/actions"
def initialize(app)
@app = app
end
def call(env)
request = ActionDispatch::Request.new(env)
return @app.call(env) unless actionable_request?(request)
ActiveSupport::ActionableError.dispatch(request.params[:error].to_s.safe_constantize, request.params[:action])
redirect_to request.params[:location]
end
private
def actionable_request?(request)
request.get_header("action_dispatch.show_detailed_exceptions") && request.post? && request.path == endpoint
end
def redirect_to(location)
uri = URI.parse location
if uri.relative? || uri.scheme == "http" || uri.scheme == "https"
body = "<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redirected</a>.</body></html>"
else
return [400, { "Content-Type" => "text/plain" }, ["Invalid redirection URI"]]
end
[302, {
"Content-Type" => "text/html; charset=#{Response.default_charset}",
"Content-Length" => body.bytesize.to_s,
"Location" => location,
}, [body]]
end
end
end