Class: Noise::ExceptionResponder
- Inherits:
-
Object
- Object
- Noise::ExceptionResponder
- Defined in:
- lib/noise/exception_responder.rb
Overview
Constructs error response (status, body)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#exception_renderer ⇒ Object
readonly
Returns the value of attribute exception_renderer.
Class Method Summary collapse
Instance Method Summary collapse
-
#body ⇒ Hash
JSON-serializable body.
- #error ⇒ Object
-
#headers ⇒ Hash
Headers.
-
#initialize(env, exception_renderer = Noise.config.exception_renderer.new(env)) ⇒ ExceptionResponder
constructor
A new instance of ExceptionResponder.
-
#status_code ⇒ Integer
HTTP status code.
Constructor Details
#initialize(env, exception_renderer = Noise.config.exception_renderer.new(env)) ⇒ ExceptionResponder
Returns a new instance of ExceptionResponder.
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_renderer ⇒ Object (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.
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
#body ⇒ Hash
Returns JSON-serializable body.
33 34 35 |
# File 'lib/noise/exception_responder.rb', line 33 def body @body ||= exception_renderer.render(self) end |
#error ⇒ Object
52 53 54 |
# File 'lib/noise/exception_responder.rb', line 52 def error env['action_dispatch.exception'] end |
#headers ⇒ Hash
Returns 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_code ⇒ Integer
Returns 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 |