class AuthorizeNet::ErrorHandler

Constants

ERROR_CODES
ERROR_FIELDS
ERROR_FIELD_REGEXES
MESSAGE_CODES

Public Class Methods

buildError(error) click to toggle source

Attempts to determine the error type and field for an error hash @param Hash error @return Hash error

# File lib/authorize_net/error_handler.rb, line 75
def buildError(error)
  code = error[:code]
  text = error[:text]
  type = getTypeFromCode(code)
  field = nil

  if !type.nil? and ERROR_FIELDS.has_key? type
    field = ERROR_FIELDS[type]
  else
    field = getFieldFromText(text)
  end

  return {
    :code => code,
    :text => text,
    :type => type,
    :field => field,
  }
end
getFieldFromText(text) click to toggle source

Attempts to determine the error field given an error message @param String text @return Symbol|nil field

# File lib/authorize_net/error_handler.rb, line 116
def getFieldFromText(text)
  if text.nil?
    return nil
  end

  ERROR_FIELD_REGEXES.each do |regex|
    field_match = text.match(regex)
    if !field_match.nil?
      field = field_match[1]

      if ERROR_FIELDS.keys.include? field
        return ERROR_FIELDS[field]
      end
      return field
    end
  end

  return nil
end
getTypeFromCode(code) click to toggle source

Attempts to determine the error type given an error code @param String code @return Symbol|nil type

# File lib/authorize_net/error_handler.rb, line 101
def getTypeFromCode(code)
  if ERROR_CODES.has_key? code
    return ERROR_CODES[code]
  elsif MESSAGE_CODES.has_key? code
    return MESSAGE_CODES[code]
  end
  return nil
end
handle(response) click to toggle source

Creates an exception, populates it as well as possible, and then raises it @param AuthorizeNet::Response @throws AuthorizeNet::Exception

# File lib/authorize_net/error_handler.rb, line 40
def handle(response)
  exception = AuthorizeNet::Exception.new

  if !response.errors.nil?
    first_error = response.errors.first
    exception.message = first_error[:text]

    # Add errors to exception
    response.errors.each do |error|
      exception.errors << buildError(error)
    end

    raise exception

  # If there are no errors, then the "messages" are probably errors... *sigh*
  elsif !response.messages.nil? and response.result == AuthorizeNet::RESULT_ERROR
    first_msg = response.messages.first
    exception.message = first_msg[:text]

    # Add messages (that are sometimes actually errors) to exception
    response.messages.each do |msg|
      exception.errors << buildError(msg)
    end

    raise exception
  end

end