class Yoti::DocScan::Error

Raises exceptions related to Doc Scan API requests

Public Class Methods

new(msg = nil, response = nil) click to toggle source
Calls superclass method
# File lib/yoti/doc_scan/errors.rb, line 9
def initialize(msg = nil, response = nil)
  super(msg, response)

  @default_message = msg
end
wrap(error) click to toggle source

Wraps an existing error

@param [Error] error

@return [self]

# File lib/yoti/doc_scan/errors.rb, line 26
def self.wrap(error)
  new(error.message, error.response)
end

Public Instance Methods

message() click to toggle source
# File lib/yoti/doc_scan/errors.rb, line 15
def message
  @message ||= format_message
end

Private Instance Methods

format_message() click to toggle source

Formats error message from response.

@return [String]

# File lib/yoti/doc_scan/errors.rb, line 37
def format_message
  return @default_message if @response.nil? || @response['Content-Type'] != 'application/json'

  json = JSON.parse(@response.body)
  format_response(json) || @default_message
end
format_property_errors(errors) click to toggle source

Format property errors.

@param [Array<Hash>] errors

@return [Array<String>]

# File lib/yoti/doc_scan/errors.rb, line 72
def format_property_errors(errors)
  errors
    .map do |e|
      "#{e['property']} \"#{e['message']}\"" if e['property'] && e['message']
    end
    .compact
end
format_response(json) click to toggle source

Format JSON error response.

@param [Hash] json

@return [String, nil]

# File lib/yoti/doc_scan/errors.rb, line 51
def format_response(json)
  return nil if json['code'].nil? || json['message'].nil?

  code_message = "#{json['code']} - #{json['message']}"

  unless json['errors'].nil?
    property_errors = format_property_errors(json['errors'])

    return "#{code_message}: #{property_errors.compact.join(', ')}" if property_errors.count.positive?
  end

  code_message
end