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.


28
29
30
31
32
# File 'lib/chatwork/chatwork_error.rb', line 28

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


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

def error_response
  @error_response
end

#statusObject (readonly)

Returns the value of attribute status


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

def status
  @status
end

Class Method Details

.from_response(status, body, headers) ⇒ Object


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

def self.from_response(status, body, headers)
  # HTTP status 204 don't have body.
  return APIError.new(status, "") if status == 204

  hash =
    begin
      JSON.parse(body)
    rescue JSON::ParserError => e
      return ChatWork::APIConnectionError.new("Response JSON is broken. #{e.message}: #{body}", e)
    end
  unless hash["errors"]
    return APIConnectionError.new("Invalid response #{body}")
  end

  if headers.has_key?("WWW-Authenticate")
    return AuthenticateError.new(headers["WWW-Authenticate"], status, hash["errors"])
  end

  APIError.new(status, hash["errors"])
end