Exception: ChatWork::ChatWorkError

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

Direct Known Subclasses

APIConnectionError, APIError

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.



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

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

Instance Attribute Details

#error_responseObject (readonly)

Returns the value of attribute error_response.



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

def error_response
  @error_response
end

#statusObject (readonly)

Returns the value of attribute status.



22
23
24
# File 'lib/chatwork/chatwork_error.rb', line 22

def status
  @status
end

Class Method Details

.from_response(status, body) ⇒ Object



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

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

  hash =
    begin
      JSON.load(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

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