Sha256: d34ba705c513f2714272d2b61d7b71c89e91590074768dcd72787ffc2321f016

Contents?: true

Size: 1.32 KB

Versions: 38

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require "erb"
require "uri"
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

Version data entries

38 entries across 36 versions & 4 rubygems

Version Path
actionpack-7.0.8.7 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.8.6 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.8.5 lib/action_dispatch/middleware/actionable_exceptions.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_dispatch/middleware/actionable_exceptions.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/middleware/actionable_exceptions.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/middleware/actionable_exceptions.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.8.4 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.8.1 lib/action_dispatch/middleware/actionable_exceptions.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/actionable_exceptions.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.8 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.7.2 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.7.1 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.7 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.6 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.5.1 lib/action_dispatch/middleware/actionable_exceptions.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/actionable_exceptions.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.0.5 lib/action_dispatch/middleware/actionable_exceptions.rb