Exception: ChatWork::ChatWorkError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/chatwork/chatwork_error.rb

Direct Known Subclasses

APIConnectionError, APIError, AuthenticateError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status = nil, error_response = nil) ⇒ ChatWorkError

Returns a new instance of ChatWorkError.



26
27
28
29
30
# File 'lib/chatwork/chatwork_error.rb', line 26

def initialize(message, status = nil, error_response = nil)
  @status = status
  @error_response = error_response
  super(message)
end

Instance Attribute Details

#error_responseObject (readonly)

Returns the value of attribute error_response.



24
25
26
# File 'lib/chatwork/chatwork_error.rb', line 24

def error_response
  @error_response
end

#statusObject (readonly)

Returns the value of attribute status.



24
25
26
# File 'lib/chatwork/chatwork_error.rb', line 24

def status
  @status
end

Class Method Details

.from_response(status, body, headers) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chatwork/chatwork_error.rb', line 3

def self.from_response(status, body, headers)
  body ||= {}

  if headers.has_key?("WWW-Authenticate")
    return AuthenticateError.from_www_authenticate(
      www_authenticate: headers["WWW-Authenticate"],
      status:           status,
      error_response:   body["errors"],
    )
  end

  return APIError.new(status, body["errors"]) if body["errors"]

  if body["error"]
    message = [body["error"], body["error_description"]].compact.join(" ")
    return AuthenticateError.new(message, status, body, body["error"], body["error_description"])
  end

  APIConnectionError.new("Invalid response #{body.to_hash} (status: #{status})")
end