Module: Clicksign::ErrorHandler
- Defined in:
- lib/clicksign/error_handler.rb
Constant Summary collapse
- ERROR_MAP =
{ [401, 403] => AuthenticationError, [404] => NotFoundError, [400, 422] => ValidationError, [409] => ConflictError, [429] => RateLimitError, }.freeze
Class Method Summary collapse
- .call(response) ⇒ Object
- .error_class_for(code) ⇒ Object
- .extract_from_json(response) ⇒ Object
- .extract_message(response) ⇒ Object
- .extract_metadata(response) ⇒ Object
Class Method Details
.call(response) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/clicksign/error_handler.rb', line 13 def self.call(response) return nil if (200..299).cover?(response.code.to_i) = (response) = (response) error_class = error_class_for(response.code.to_i) raise error_class.new(, **) end |
.error_class_for(code) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/clicksign/error_handler.rb', line 22 def self.error_class_for(code) ERROR_MAP.each { |codes, klass| return klass if codes.include?(code) } return ServerError if (500..599).cover?(code) Error end |
.extract_from_json(response) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/clicksign/error_handler.rb', line 46 def self.extract_from_json(response) body = JSON.parse(response.body) return response. unless body.is_a?(Hash) errors = body['errors'] return response. unless errors.is_a?(Array) errors.filter_map { |e| e['detail'] || e['title'] }.join(', ') end |
.extract_message(response) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/clicksign/error_handler.rb', line 38 def self.(response) return response. if response.body.nil? || response.body.empty? extract_from_json(response) rescue JSON::ParserError response. end |
.extract_metadata(response) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/clicksign/error_handler.rb', line 29 def self.(response) { status_code: response.code.to_i, request_id: response['x-request-id'], response_body: response.body, response_headers: response.each_header.to_h, } end |