Exception: APIException

Inherits:
StandardError
  • Object
show all
Defined in:
lib/carehq/exceptions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, hint = nil, arg_errors = nil) ⇒ APIException

Returns a new instance of APIException.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/carehq/exceptions.rb', line 10

def initialize(status_code, hint=nil, arg_errors=nil)

    # The status code associated with the error
    @status_code = status_code

    # A hint providing additional information as to why this error
    # occurred.
    @hint = hint

    # A dictionary of errors relating to the arguments (parameters) sent
    # to the API endpoint (e.g `{'arg_name': ['error1', ...]}`).
    @arg_errors = arg_errors

    super()
end

Instance Attribute Details

#arg_errorsObject (readonly)

Returns the value of attribute arg_errors.



8
9
10
# File 'lib/carehq/exceptions.rb', line 8

def arg_errors
  @arg_errors
end

#hintObject (readonly)

Returns the value of attribute hint.



7
8
9
# File 'lib/carehq/exceptions.rb', line 7

def hint
  @hint
end

#status_codeObject (readonly)

An error occurred while processing an API the request.



6
7
8
# File 'lib/carehq/exceptions.rb', line 6

def status_code
  @status_code
end

Class Method Details

.get_class_by_status_code(error_type, default = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/carehq/exceptions.rb', line 26

def APIException.get_class_by_status_code(error_type, default=nil)

    class_map = {
        400 => InvalidRequest,
        401 => Unauthorized,
        403 => Forbidden,
        405 => Forbidden,
        404 => NotFound,
        429 => RateLimitExceeded
    }

    if class_map.has_key? error_type
        return class_map[error_type]

    elsif default
        return default

    end

    return APIException

end