Repository: rails/rails
File: activesupport/lib/active_support/error_reporter.rb (Ruby)

Code before:
        begin
          # We could use Exception#set_backtrace, but until Ruby 3.4
          # it only support setting `Exception#backtrace` and not
          # `Exception#backtrace_locations`. So raising the exception
          # is a good way to build a real backtrace.
          raise error
        rescue error.class => error
        end

        count = 0
        while error.backtrace_locations.first&.path == __FILE__
          count += 1
          error.backtrace_locations.shift
        end

        error.backtrace.shift(count)
      end

      # Contains a stack of callables to run on an error's execution context before passing
      # to subscribers. Allows creation of entries in error context that are shared by all
      # subscribers.
      #
      # A context middleware receives the error and current state of the context hash.
      # It must return a hash - the middleware stack returns the hash after it has
      # run through all middlewares. A middleware can mutate or replace the hash.
      class ErrorContextMiddlewareStack
        def initialize
          @stack = []
        end

        # Add a middleware to the error context stack.
        def use(middleware)
          unless middleware.respond_to?(:call)
            raise ArgumentError, "Error context middleware must respond to #call"
          end

          @stack << middleware
        end

        # Run all middlewares in the stack on an error and context.
        def execute(error, context)
          @stack.inject(context) { |c, middleware| middleware.call(error, c) }
        end
      end
  end
end


Review comment (suggestion):
```suggestion
      class ErrorContextMiddlewareStack # :nodoc:
```

Since it's an implementation detail.

Code after:

      while error
        unless error.frozen?
          error.instance_variable_set(:@__rails_error_reported, true)
        end
        error = error.cause
      end

      nil
    end

    private
      def ensure_backtrace(error)
        return if error.frozen? # re-raising won't add a backtrace
        return unless error.backtrace.nil?

        begin
          # We could use Exception#set_backtrace, but until Ruby 3.4
          # it only support setting `Exception#backtrace` and not
          # `Exception#backtrace_locations`. So raising the exception
          # is a good way to build a real backtrace.
          raise error
        rescue error.class => error
        end

        count = 0
        while error.backtrace_locations.first&.path == __FILE__
          count += 1
          error.backtrace_locations.shift
        end

        error.backtrace.shift(count)
      end

      class ErrorContextMiddlewareStack # :nodoc:
        def initialize
          @stack = []
        end

        # Add a middleware to the error context stack.
        def use(middleware)
          unless middleware.respond_to?(:call)
            raise ArgumentError, "Error context middleware must respond to #call"
          end

          @stack << middleware
        end

        # Run all middlewares in the stack on an error and context.
        def execute(error, context)
          @stack.inject(context) { |c, middleware| middleware.call(error, c) }