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.



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

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

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

.generate(response) ⇒ Object



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

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

Instance Method Details

#codesObject



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

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

#detailsObject



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

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

#errorsObject



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

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

#full_messageObject



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

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



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

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