Exception: Conexa::ResponseError

Inherits:
ConexaError show all
Defined in:
lib/conexa/errors.rb

Direct Known Subclasses

NotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_params, error, message = nil, api_response = nil) ⇒ ResponseError

Returns a new instance of ResponseError.



31
32
33
34
35
36
37
# File 'lib/conexa/errors.rb', line 31

def initialize(request_params, error, message=nil, api_response=nil)
  @request_params, @error = request_params, error
  @api_response = api_response.is_a?(Hash) ? api_response : {}
  msg = describe_error(error)
  msg +=  " => " + message if message
  super msg
end

Instance Attribute Details

#api_responseHash (readonly)

The decoded error body, when the API returned one.

Returns:

  • (Hash)


29
30
31
# File 'lib/conexa/errors.rb', line 29

def api_response
  @api_response
end

#errorObject (readonly)

Returns the value of attribute error.



25
26
27
# File 'lib/conexa/errors.rb', line 25

def error
  @error
end

#request_paramsObject (readonly)

Returns the value of attribute request_params.



25
26
27
# File 'lib/conexa/errors.rb', line 25

def request_params
  @request_params
end

Instance Method Details

#api_error_codesArray<String>

Documented business-rule codes, e.g. "CHARGE_11" or "CONTRACT_RECURRING_SALE_10". These are what a caller branches on — for instance to tell an already-settled charge from a real settlement failure.

Returns:

  • (Array<String>)


62
63
64
# File 'lib/conexa/errors.rb', line 62

def api_error_codes
  api_errors.filter_map { |entry| entry[:code] }
end

#api_error_messagesArray<String>

One readable line per error, whichever shape it arrived in.

Returns:

  • (Array<String>)


68
69
70
71
72
73
# File 'lib/conexa/errors.rb', line 68

def api_error_messages
  api_errors.map do |entry|
    label = entry[:field] || entry[:code]
    label ? "#{label}: #{entry[:message]}" : entry[:message]
  end
end

#api_errorsArray<Hash{Symbol=>String,nil}>

The API's errors, normalised across its two shapes.

Field validation answers {"field": …, "messages": [...]}; business rules answer {"code": …, "message": …}. Consumers that only handled the first rendered business-rule errors as blank strings — which is how CONTRACT_RECURRING_SALE_10 stayed invisible through eight attempts.

Returns:

  • (Array<Hash{Symbol=>String,nil}>)

    entries of code:, message:



47
48
49
50
51
52
53
54
55
# File 'lib/conexa/errors.rb', line 47

def api_errors
  Array(api_response["errors"]).filter_map do |entry|
    next unless entry.is_a?(Hash)

    { field:   entry["field"],
      code:    entry["code"],
      message: entry["message"] || Array(entry["messages"]).join("; ") }
  end
end