Exception: Particle::Error

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

Overview

Custom error class for rescuing from all Particle errors

Direct Known Subclasses

ClientError, MissingTokenError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



28
29
30
31
32
# File 'lib/particle/error.rb', line 28

def initialize(response=nil)
  @response = response
  @short_message = build_short_message
  super(build_error_message)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



34
35
36
# File 'lib/particle/error.rb', line 34

def response
  @response
end

#short_messageObject (readonly)

Returns the value of attribute short_message.



35
36
37
# File 'lib/particle/error.rb', line 35

def short_message
  @short_message
end

Class Method Details

.from_response(response) ⇒ Particle::Error

Returns the appropriate Particle::Error subclass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/particle/error.rb', line 12

def self.from_response(response)
  status  = response[:status].to_i
  body    = response[:body].to_s

  if klass =  case status
              when 400      then bad_request_error(body)
              when 401      then Particle::Unauthorized
              when 403      then Particle::Forbidden
              when 404      then Particle::NotFound
              when 408      then Particle::TimedOut
              when 500..599 then Particle::ServerError
              end
    klass.new(response)
  end
end