Class: BatchApi::ErrorWrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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)?

Returns:

  • (Boolean)


42
43
44
# File 'lib/batch_api/error_wrapper.rb', line 42

def self.expose_backtrace?
  !Rails.env.production?
end

Instance Method Details

#bodyObject

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
  message = if self.class.expose_backtrace?
    {
      message: @error.message,
      backtrace: @error.backtrace
    }
  else
    { message: @error.message }
  end
  { error: message }
end

#renderObject

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_codeObject

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