Class: FolioClient::UnexpectedResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/folio_client/unexpected_response.rb

Overview

Handles unexpected responses when communicating with Folio

Class Method Summary collapse

Class Method Details

.call(response, **kwargs) ⇒ Object

Parameters:

  • response (Faraday::Response)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/folio_client/unexpected_response.rb', line 7

def self.call(response, **kwargs) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  subject = kwargs.fetch(:exception_subject, 'resource')

  case response.status
  when 400
    raise BadRequestError, "Bad request for #{subject}: #{response.body}"
  when 401
    raise UnauthorizedError, "There was a problem with the access token: #{response.body}"
  when 403
    raise ForbiddenError, "The operation requires privileges which the client does not have: #{response.body}"
  when 404
    raise ResourceNotFound, "Endpoint not found or #{subject} does not exist: #{response.body}"
  when 409
    raise ConflictError, "Resource cannot be updated: #{response.body}"
  when 422
    raise ValidationError, "There was a validation problem with the request: #{response.body}"
  when 500
    raise ServiceUnavailable, "The remote server returned an internal server error: #{response.body}"
  else
    raise Error, "Unexpected response: #{response.status} #{response.body}"
  end
end