Module: Clicksign::ErrorHandler

Defined in:
lib/clicksign/error_handler.rb

Class Method Summary collapse

Class Method Details

.call(response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/clicksign/error_handler.rb', line 5

def self.call(response)
  case response.code.to_i
  when 200..299 then nil
  when 401, 403 then raise AuthenticationError, extract_message(response)
  when 404      then raise NotFoundError,        extract_message(response)
  when 400, 422 then raise ValidationError,      extract_message(response)
  when 409      then raise ConflictError,         extract_message(response)
  when 429      then raise RateLimitError,        extract_message(response)
  when 500..599 then raise ServerError,           extract_message(response)
  else               raise Error,                 extract_message(response)
  end
end

.extract_message(response) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clicksign/error_handler.rb', line 18

def self.extract_message(response)
  body = JSON.parse(response.body)
  return response.message unless body.is_a?(Hash)

  errors = body['errors']
  return response.message if errors.nil? || !errors.is_a?(Array)

  errors.filter_map { |e| e['detail'] || e['title'] }.join(', ')
rescue JSON::ParserError
  response.message
end