Exception: Judopay::APIError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/judopay/error.rb

Overview

Custom error class for rescuing from all API errors

Constant Summary collapse

CATEGORY_UNKNOWN =
0
CATEGORY_REQUEST =
1
CATEGORY_MODEL =
2
CATEGORY_CONFIG =
3
CATEGORY_PROCESSING =
4
CATEGORY_EXCEPTION =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, error_code = 0, status_code = 0, category = CATEGORY_UNKNOWN, field_errors = []) ⇒ APIError

Returns a new instance of APIError.



63
64
65
66
67
68
69
# File 'lib/judopay/error.rb', line 63

def initialize(message, error_code = 0, status_code = 0, category = CATEGORY_UNKNOWN, field_errors = [])
  @message = message
  @error_code = error_code
  @status_code = status_code
  @category = category
  @field_errors = field_errors
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



29
30
31
# File 'lib/judopay/error.rb', line 29

def category
  @category
end

#error_codeObject

Returns the value of attribute error_code.



29
30
31
# File 'lib/judopay/error.rb', line 29

def error_code
  @error_code
end

#field_errorsObject

Returns the value of attribute field_errors.



29
30
31
# File 'lib/judopay/error.rb', line 29

def field_errors
  @field_errors
end

#messageObject



75
76
77
# File 'lib/judopay/error.rb', line 75

def message
  (@message || self.class.name) + field_errors_message
end

#status_codeObject

Returns the value of attribute status_code.



29
30
31
# File 'lib/judopay/error.rb', line 29

def status_code
  @status_code
end

Class Method Details

.factory(response) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/judopay/error.rb', line 34

def factory(response)
  parsed_body = JSON.parse(response.body)

  new(
    parsed_body['message'],
    parsed_body['code'],
    response.status.to_i,
    parsed_body['category'],
    extract_field_errors(parsed_body['details'])
  )
end

Instance Method Details

#to_sObject



71
72
73
# File 'lib/judopay/error.rb', line 71

def to_s
  "JudoPay ApiException (status code #{@status_code}, error code #{@error_code}, category #{@category}) #{message}"
end