Exception: RelayGrid::APIError

Inherits:
Error
  • Object
show all
Defined in:
lib/relaygrid/errors.rb

Overview

The server answered, but not with success.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status: nil, body: nil) ⇒ APIError

Returns a new instance of APIError.



24
25
26
27
28
# File 'lib/relaygrid/errors.rb', line 24

def initialize(message = nil, status: nil, body: nil)
  @status = status
  @body = body
  super(message || default_message)
end

Instance Attribute Details

#bodyHash, ... (readonly)

Returns parsed response body.

Returns:

  • (Hash, String, nil)

    parsed response body



22
23
24
# File 'lib/relaygrid/errors.rb', line 22

def body
  @body
end

#statusInteger? (readonly)

Returns HTTP status.

Returns:

  • (Integer, nil)

    HTTP status



20
21
22
# File 'lib/relaygrid/errors.rb', line 20

def status
  @status
end

Class Method Details

.from_response(status, body) ⇒ Object

Maps an HTTP response onto the right subclass. 404s are discriminated further by the server's message so callers can tell "no such template" from "no such user" without string-matching themselves.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/relaygrid/errors.rb', line 38

def self.from_response(status, body)
  klass =
    case status
    when 401, 403 then AuthenticationError
    when 402 then LimitExceededError
    when 404 then not_found_class(body)
    when 422 then ValidationError
    when 400..499 then APIError
    when 500..599 then ServerError
    else APIError
    end

  klass.new(status: status, body: body)
end

Instance Method Details

#error_messageObject

The server's own error string when it sent one.



31
32
33
# File 'lib/relaygrid/errors.rb', line 31

def error_message
  body["error"] if body.is_a?(Hash)
end