Exception: SimpleJSONAPIClient::Errors::APIError

Inherits:
SimpleJSONAPIClient::Error show all
Extended by:
Forwardable
Defined in:
lib/simple_jsonapi_client/errors/api_error.rb

Constant Summary collapse

KNOWN_ERRORS =
{
  400 => 'BadRequestError',
  404 => 'NotFoundError',
  422 => 'UnprocessableEntityError'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ APIError

Returns a new instance of APIError.



23
24
25
26
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 23

def initialize(response)
  @response = response
  super(full_message)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



20
21
22
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 20

def response
  @response
end

Class Method Details

.generate(response) ⇒ Object



15
16
17
18
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 15

def self.generate(response)
  error = KNOWN_ERRORS[response.status]
  SimpleJSONAPIClient::Errors.const_get(error).new(response)
end

Instance Method Details

#codesObject



47
48
49
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 47

def codes
  @codes ||= errors.map { |error| error['code'] }.compact
end

#detailsObject



51
52
53
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 51

def details
  @details ||= errors.map { |error| error['detail'] }.compact
end

#errorsObject



28
29
30
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 28

def errors
  Array(body['errors'])
end

#full_messageObject



42
43
44
45
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 42

def full_message
  "The API returned a #{status} error status and this content:\n" +
    pretty_printed_response.each_line.map { |line| "  #{line}" }.join
end

#messageObject



32
33
34
35
36
37
38
39
40
# File 'lib/simple_jsonapi_client/errors/api_error.rb', line 32

def message
  if !codes.empty?
    codes_message
  elsif !details.empty?
    details_message
  else
    default_message
  end
end