Class: Noise::ExceptionResponder

Inherits:
Object
  • Object
show all
Defined in:
lib/noise/exception_responder.rb

Overview

Constructs error response (status, body)

Direct Known Subclasses

RateLimitErrorResponder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, exception_renderer = Noise.config.exception_renderer.new(env)) ⇒ ExceptionResponder

Returns a new instance of ExceptionResponder.

Parameters:

  • env (Hash)

    rack env

  • exception_renderer (ExceptionRenderer) (defaults to: Noise.config.exception_renderer.new(env))


24
25
26
27
# File 'lib/noise/exception_responder.rb', line 24

def initialize(env, exception_renderer = Noise.config.exception_renderer.new(env))
  @env = env
  @exception_renderer = exception_renderer
end

Instance Attribute Details

#exception_rendererObject (readonly)

Returns the value of attribute exception_renderer.



29
30
31
# File 'lib/noise/exception_responder.rb', line 29

def exception_renderer
  @exception_renderer
end

Class Method Details

.register(error, status:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • error (StandardError)
  • status (Integer, Symbol)

    HTTP status to use for response



17
18
19
# File 'lib/noise/exception_responder.rb', line 17

def register(error, status:)
  ActionDispatch::ExceptionWrapper.rescue_responses[error.to_s] = status
end

Instance Method Details

#bodyHash

Returns JSON-serializable body.

Returns:

  • (Hash)

    JSON-serializable body



33
34
35
# File 'lib/noise/exception_responder.rb', line 33

def body
  @body ||= exception_renderer.render(self)
end

#errorObject



52
53
54
# File 'lib/noise/exception_responder.rb', line 52

def error
  env['action_dispatch.exception']
end

#headersHash

Returns headers.

Returns:

  • (Hash)

    headers



38
39
40
41
42
43
# File 'lib/noise/exception_responder.rb', line 38

def headers
  {
    'Content-Type' => "#{::Mime[:json]}; charset=#{ActionDispatch::Response.default_charset}",
    'Content-Length' => body.bytesize.to_s,
  }
end

#status_codeInteger

Returns HTTP status code.

Returns:

  • (Integer)

    HTTP status code



46
47
48
49
50
# File 'lib/noise/exception_responder.rb', line 46

def status_code
  status_symbol = ActionDispatch::ExceptionWrapper.rescue_responses[error.class.name]
  # calls `status_code` from Rack::Utils
  Rack::Utils.status_code(status_symbol)
end