Class: BatchApi::Errors::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_api/errors/base.rb

Direct Known Subclasses

Operation, Request

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Base

Public: create a new BatchError from a Rails error.



8
9
10
# File 'lib/batch_api/errors/base.rb', line 8

def initialize(error)
  @error = error
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)


40
41
42
# File 'lib/batch_api/errors/base.rb', line 40

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.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/batch_api/errors/base.rb', line 14

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.



29
30
31
# File 'lib/batch_api/errors/base.rb', line 29

def render
  [status_code, Middleware.content_type, [MultiJson.dump(body)]]
end

#status_codeObject

Public: the status code to return for the given error.



34
35
36
# File 'lib/batch_api/errors/base.rb', line 34

def status_code
  500
end