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.



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

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.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Class Method Details

.error_class_for(status) ⇒ Object



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

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



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

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