Exception: Locomotive::Coal::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/locomotive/coal/error.rb

Overview

Take inspiration from this repository: github.com/vigeland/hooty

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



10
11
12
13
# File 'lib/locomotive/coal/error.rb', line 10

def initialize(response = nil)
  @response = response
  super(build_message)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/locomotive/coal/error.rb', line 8

def response
  @response
end

Class Method Details

.from_response(response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/locomotive/coal/error.rb', line 19

def self.from_response(response)
  status = response.status
  if klass = case status.to_i
              when 401      then Locomotive::Coal::UnauthorizedError
              when 404      then Locomotive::Coal::UnknownResourceError
              when 413      then Locomotive::Coal::FileTooBigError
              when 422      then Locomotive::Coal::InvalidResourceError
              when 429      then Locomotive::Coal::TooManyRequestsError
              when 500      then Locomotive::Coal::ServerSideError
              else Error
              end
    klass.new(response)
  end
end

Instance Method Details

#bodyObject



15
16
17
# File 'lib/locomotive/coal/error.rb', line 15

def body
  @response.body || {}
end