Class: Grape::Exceptions::ErrorResponse

Inherits:
Data
  • Object
show all
Defined in:
lib/grape/exceptions/error_response.rb

Overview

Value object representing the payload thrown via ‘throw :error, …` and consumed by `Middleware::Error#error_response`. Replaces the implicit-schema Hash that previously circulated between throw sites and the error middleware.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: nil, message: nil, headers: nil, backtrace: nil, original_exception: nil) ⇒ ErrorResponse

Returns a new instance of ErrorResponse.



10
11
12
# File 'lib/grape/exceptions/error_response.rb', line 10

def initialize(status: nil, message: nil, headers: nil, backtrace: nil, original_exception: nil)
  super
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace

Returns:

  • (Object)

    the current value of backtrace



9
10
11
# File 'lib/grape/exceptions/error_response.rb', line 9

def backtrace
  @backtrace
end

#headersObject (readonly)

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



9
10
11
# File 'lib/grape/exceptions/error_response.rb', line 9

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



9
10
11
# File 'lib/grape/exceptions/error_response.rb', line 9

def message
  @message
end

#original_exceptionObject (readonly)

Returns the value of attribute original_exception

Returns:

  • (Object)

    the current value of original_exception



9
10
11
# File 'lib/grape/exceptions/error_response.rb', line 9

def original_exception
  @original_exception
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



9
10
11
# File 'lib/grape/exceptions/error_response.rb', line 9

def status
  @status
end

Class Method Details

.coerce(input) ⇒ Object

Normalize heterogeneous inputs into an ErrorResponse. Preserves the public contract that users can still ‘throw :error, hash` from their own middleware or `rescue_from` handlers.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/grape/exceptions/error_response.rb', line 31

def self.coerce(input)
  case input
  when ErrorResponse
    input
  when Grape::Exceptions::Base
    from_exception(input)
  when Hash
    new(**input.slice(:status, :message, :headers, :backtrace, :original_exception))
  else
    new
  end
end

.from_exception(exception) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/grape/exceptions/error_response.rb', line 18

def self.from_exception(exception)
  new(
    status: exception.status,
    message: exception.message,
    headers: exception.headers,
    backtrace: exception.backtrace,
    original_exception: exception
  )
end

Instance Method Details

#to_sObject



14
15
16
# File 'lib/grape/exceptions/error_response.rb', line 14

def to_s
  "#<#{self.class.name} status=#{status.inspect} message=#{message.inspect} headers=#{headers.inspect}>"
end