Module: Jets::Api::Error::Handlers

Included in:
Client
Defined in:
lib/jets/api/error.rb

Instance Method Summary collapse

Instance Method Details

#general_api_error(message, http_status) ⇒ Object



32
33
34
# File 'lib/jets/api/error.rb', line 32

def general_api_error(message, http_status)
  Error.new(message, http_status: http_status)
end

#handle_as_error?(http_status) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/jets/api/error.rb', line 16

def handle_as_error?(http_status)
  http_status >= 400
end

#handle_error_response!(resp) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jets/api/error.rb', line 20

def handle_error_response!(resp)
  error = if resp.data[:error].nil?
    general_api_error("Indeterminate error", resp.http_status)
  elsif resp.data[:error].is_a?(String) # Internal Server Error
    general_api_error(resp.data[:error], resp.http_status)
  else
    specific_api_error(resp)
  end

  raise error
end

#specific_api_error(resp) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jets/api/error.rb', line 36

def specific_api_error(resp)
  message = resp.data[:error][:message]
  http_status = resp.http_status

  error_class = Jets::Api::Error.http_errors[http_status]
  if error_class
    Jets::Api::Error.const_get(error_class).new(message, http_status: http_status)
  else
    general_api_error(message, http_status)
  end
end