Exception: KeycloakAdmin::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/keycloak-admin/error.rb

Overview

Raised when Keycloak answers with a non-2xx status.

Callers can rescue the precise failure (KeycloakAdmin::NotFoundError), a whole family (KeycloakAdmin::ClientError for any 4xx) or every HTTP failure (KeycloakAdmin::ApiError), and read #status/#body off the exception rather than parsing the message.

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, body, headers = {}) ⇒ ApiError

Returns a new instance of ApiError.



12
13
14
15
16
17
# File 'lib/keycloak-admin/error.rb', line 12

def initialize(status, body, headers = {})
  @status  = status
  @body    = body
  @headers = headers || {}
  super("Keycloak: The request failed with response code #{status} and message: #{body}")
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/keycloak-admin/error.rb', line 10

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/keycloak-admin/error.rb', line 10

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/keycloak-admin/error.rb', line 10

def status
  @status
end

Class Method Details

.error_class_for(status) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/keycloak-admin/error.rb', line 25

def self.error_class_for(status)
  case status
  when 400      then BadRequestError
  when 401      then UnauthorizedError
  when 403      then ForbiddenError
  when 404      then NotFoundError
  when 409      then ConflictError
  when 400..499 then ClientError
  when 500..599 then ServerError
  else ApiError
  end
end

.from_response(response) ⇒ Object



19
20
21
22
23
# File 'lib/keycloak-admin/error.rb', line 19

def self.from_response(response)
  response ||= {}
  status     = response[:status]
  error_class_for(status).new(status, response[:body], response[:headers])
end