Module: GrapeRailsLogger::StatusExtractor

Defined in:
lib/grape_rails_logger/status_extractor.rb

Overview

Shared utility for extracting HTTP status codes from exceptions

Constant Summary collapse

EXCEPTION_STATUS_MAP =

Common exception to status code mappings

{
  "ActiveRecord::RecordNotFound" => 404,
  "ActiveRecord::RecordNotUnique" => 409,
  "ActiveRecord::RecordInvalid" => 422,
  "ActiveRecord::StatementInvalid" => 422,
  "ActionController::RoutingError" => 404,
  "ActionController::MethodNotAllowed" => 405,
  "ActionController::NotImplemented" => 501,
  "ActionController::UnknownFormat" => 406,
  "ActionController::BadRequest" => 400,
  "ActionController::ParameterMissing" => 400
}.freeze

Class Method Summary collapse

Class Method Details

.extract_status_from_exception(e) ⇒ Integer

Extracts HTTP status code from an exception

Parameters:

  • e (Exception)

    The exception to extract status from

Returns:

  • (Integer)

    HTTP status code, defaults to 500



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/grape_rails_logger/status_extractor.rb', line 24

def extract_status_from_exception(e)
  return e.status if e.respond_to?(:status) && e.status.is_a?(Integer)

  if e.instance_variable_defined?(:@status)
    status = e.instance_variable_get(:@status)
    return status if status.is_a?(Integer)
  end

  if e.respond_to?(:options) && e.options.is_a?(Hash)
    return e.options[:status] if e.options[:status].is_a?(Integer)
  end

  # Check common exception type mappings
  exception_class_name = e.class.name
  return EXCEPTION_STATUS_MAP[exception_class_name] if EXCEPTION_STATUS_MAP.key?(exception_class_name)

  # Check if any ancestor class matches
  EXCEPTION_STATUS_MAP.each do |exception_name, status_code|
    # Use safe_constantize if available (ActiveSupport), otherwise constantize
    exception_class = if exception_name.respond_to?(:safe_constantize)
      exception_name.safe_constantize
    else
      exception_name.constantize
    end
    return status_code if exception_class && e.is_a?(exception_class)
  rescue NameError
    # Exception class not available, skip
  end

  500
end