Sha256: 3fbdc5c56cd153d04ef7dfb8aad0af5c99231c3a47fb78f307ae9deef879fc31

Contents?: true

Size: 1.25 KB

Versions: 16

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

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 = ""
        else
          return [400, { Rack::CONTENT_TYPE => "text/plain; charset=utf-8" }, ["Invalid redirection URI"]]
        end

        [302, {
          Rack::CONTENT_TYPE => "text/html; charset=#{Response.default_charset}",
          Rack::CONTENT_LENGTH => body.bytesize.to_s,
          ActionDispatch::Constants::LOCATION => location,
        }, [body]]
      end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
actionpack-7.1.5 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.4.2 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.4.1 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.4 lib/action_dispatch/middleware/actionable_exceptions.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.1.3.4/lib/action_dispatch/middleware/actionable_exceptions.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actionpack-7.1.3.4/lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.3.4 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.3.2 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.3.1 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.3 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.2 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.1 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.0 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.0.rc2 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.0.rc1 lib/action_dispatch/middleware/actionable_exceptions.rb
actionpack-7.1.0.beta1 lib/action_dispatch/middleware/actionable_exceptions.rb