Repository: rails/rails
File: actionpack/lib/action_dispatch/middleware/exception_wrapper.rb (Ruby)

Code before:
    def framework_trace
      clean_backtrace(:noise)
    end

    def full_trace
      clean_backtrace(:all)
    end

    def traces
      application_trace_with_ids = []
      framework_trace_with_ids = []
      full_trace_with_ids = []
      application_traces = application_trace.map(&:to_s)

      full_trace = backtrace_cleaner&.clean_locations(backtrace, :all) || backtrace
      full_trace.each_with_index do |trace, idx|
        filtered_trace = backtrace_cleaner&.clean_frame(trace, :all) || trace

        trace_with_id = {
          exception_object_id: @exception.object_id,
          id: idx,
          trace: trace,
          filtered_trace: filtered_trace,
        }

        if application_traces.include?(filtered_trace.to_s)
          application_trace_with_ids << trace_with_id
        else
          framework_trace_with_ids << trace_with_id
        end

        full_trace_with_ids << trace_with_id
      end

      {
        "Application Trace" => application_trace_with_ids,
        "Framework Trace" => framework_trace_with_ids,
        "Full Trace" => full_trace_with_ids
      }
    end

    def self.status_code_for_exception(class_name)
      Rack::Utils.status_code(@@rescue_responses[class_name])
    end

    def show?(request)
      # We're treating `nil` as "unset", and we want the default setting to be `:all`.
      # This logic should be extracted to `env_config` and calculated once.
      config = request.get_header("action_dispatch.show_exceptions")

      case config

Review comment (suggestion):
Not a huge concern because it's an error page, but casting with `to_s` on both side is wasteful. It's much cheaper to compare two `Backtrace::Location` objects, than two long strings.

Code after:
    def framework_trace
      clean_backtrace(:noise)
    end

    def full_trace
      clean_backtrace(:all)
    end

    def traces
      application_trace_with_ids = []
      framework_trace_with_ids = []
      full_trace_with_ids = []
      application_traces = application_trace.map(&:to_s)

      full_trace = backtrace_cleaner&.clean_locations(backtrace, :all).presence || backtrace
      full_trace.each_with_index do |trace, idx|
        filtered_trace = backtrace_cleaner&.clean_frame(trace, :all) || trace

        trace_with_id = {
          exception_object_id: @exception.object_id,
          id: idx,
          trace: trace,
          filtered_trace: filtered_trace,
        }

        if application_traces.include?(filtered_trace.to_s)
          application_trace_with_ids << trace_with_id
        else
          framework_trace_with_ids << trace_with_id
        end

        full_trace_with_ids << trace_with_id
      end

      {
        "Application Trace" => application_trace_with_ids,
        "Framework Trace" => framework_trace_with_ids,
        "Full Trace" => full_trace_with_ids
      }
    end

    def self.status_code_for_exception(class_name)
      Rack::Utils.status_code(@@rescue_responses[class_name])
    end

    def show?(request)
      # We're treating `nil` as "unset", and we want the default setting to be `:all`.
      # This logic should be extracted to `env_config` and calculated once.
      config = request.get_header("action_dispatch.show_exceptions")

      case config