Exception: BetterAuth::APIError

Inherits:
Error
  • Object
show all
Defined in:
lib/better_auth/api_error.rb

Constant Summary collapse

STATUS_CODES =
{
  "BAD_REQUEST" => 400,
  "UNAUTHORIZED" => 401,
  "UNAUTHORIZED_INVALID_OR_EXPIRED_NONCE" => 401,
  "FORBIDDEN" => 403,
  "CONFLICT" => 409,
  "NOT_FOUND" => 404,
  "METHOD_NOT_ALLOWED" => 405,
  "UNPROCESSABLE_ENTITY" => 422,
  "TOO_MANY_REQUESTS" => 429,
  "BAD_GATEWAY" => 502,
  "NOT_IMPLEMENTED" => 501,
  "FOUND" => 302,
  "INTERNAL_SERVER_ERROR" => 500
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, message: nil, headers: {}, code: nil, body: nil) ⇒ APIError

Returns a new instance of APIError.



23
24
25
26
27
28
29
30
# File 'lib/better_auth/api_error.rb', line 23

def initialize(status, message: nil, headers: {}, code: nil, body: nil)
  @status = status.to_s.upcase
  @status_code = STATUS_CODES.fetch(@status, 500)
  @headers = normalize_headers(headers)
  @code = code || @status
  @body = body
  super(message || default_message)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



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

def status_code
  @status_code
end

Instance Method Details

#to_hObject



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

def to_h
  return body if body

  {
    code: code,
    message: message
  }
end