Class: BatchApi::ErrorWrapper
- Inherits:
-
Object
- Object
- BatchApi::ErrorWrapper
- Defined in:
- lib/batch_api/error_wrapper.rb
Overview
Public: wrap an error thrown during a batch operation. This has a body class and a cookies accessor and can function in place of a regular BatchResponse object.
Class Method Summary collapse
-
.expose_backtrace? ⇒ Boolean
Internal: whether the backtrace should be exposed in the response.
Instance Method Summary collapse
-
#body ⇒ Object
Public: the error details as a hash, which can be returned to clients as JSON.
-
#initialize(error) ⇒ ErrorWrapper
constructor
Public: create a new ErrorWrapper from an error object.
-
#render ⇒ Object
Public: turn the error body into a Rack-compatible body component.
-
#status_code ⇒ Object
Public: the status code to return for the given error.
Constructor Details
#initialize(error) ⇒ ErrorWrapper
Public: create a new ErrorWrapper from an error object.
9 10 11 12 |
# File 'lib/batch_api/error_wrapper.rb', line 9 def initialize(error) @error = error @status_code = error.status_code if error.respond_to?(:status_code) end |
Class Method Details
.expose_backtrace? ⇒ Boolean
Internal: whether the backtrace should be exposed in the response. Currently Rails-specific, needs to be generalized (to ENV)?
42 43 44 |
# File 'lib/batch_api/error_wrapper.rb', line 42 def self.expose_backtrace? !Rails.env.production? end |
Instance Method Details
#body ⇒ Object
Public: the error details as a hash, which can be returned to clients as JSON.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/batch_api/error_wrapper.rb', line 16 def body = if self.class.expose_backtrace? { message: @error., backtrace: @error.backtrace } else { message: @error. } end { error: } end |
#render ⇒ Object
Public: turn the error body into a Rack-compatible body component.
Returns: an Array with the error body represented as JSON.
31 32 33 |
# File 'lib/batch_api/error_wrapper.rb', line 31 def render [status_code, RackMiddleware.content_type, [MultiJson.dump(body)]] end |
#status_code ⇒ Object
Public: the status code to return for the given error.
36 37 38 |
# File 'lib/batch_api/error_wrapper.rb', line 36 def status_code @status_code || 500 end |