Exception: SimpleOAuth::OAuth2::Error

Inherits:
Error
  • Object
show all
Defined in:
lib/simple_oauth/oauth2/error.rb,
sig/simple_oauth/oauth2.rbs

Overview

Error returned by an OAuth 2.0 endpoint

Constant Summary collapse

INVALID_STATUS =

The error message for a status that is not an HTTP status

Returns:

  • (String)
"The status must be an Integer or a String of digits"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, description: nil, uri: nil, status: nil) ⇒ Error

Initialize a new error

Examples:

SimpleOAuth::OAuth2::Error.new(code: "invalid_grant", status: 400)

Parameters:

  • code (String, nil)

    the error code

  • description (String, nil) (defaults to: nil)

    the human-readable description

  • uri (String, nil) (defaults to: nil)

    the URI of a page describing the error

  • status (Integer, nil) (defaults to: nil)

    the HTTP status of the response

  • code: (String, nil)
  • description: (String, nil) (defaults to: nil)
  • uri: (String, nil) (defaults to: nil)
  • status: (Integer, nil) (defaults to: nil)


85
86
87
88
89
90
91
92
# File 'lib/simple_oauth/oauth2/error.rb', line 85

def initialize(code:, description: nil, uri: nil, status: nil)
  @code = code
  @description = description
  @uri = uri
  @status = status
  details = [code, description].compact
  super(details.empty? ? "OAuth 2.0 request failed with status #{status}" : details.join(": "))
end

Instance Attribute Details

#codeString? (readonly)

The error code, such as invalid_grant, if the response included one

Examples:

error.code # => "invalid_grant"

Returns:

  • (String, nil)

    the error code



23
24
25
# File 'lib/simple_oauth/oauth2/error.rb', line 23

def code
  @code
end

#descriptionString? (readonly)

The human-readable description from the endpoint

Examples:

error.description # => "The refresh token is invalid"

Returns:

  • (String, nil)

    the description



31
32
33
# File 'lib/simple_oauth/oauth2/error.rb', line 31

def description
  @description
end

#statusInteger? (readonly)

The HTTP status of the response

Examples:

error.status # => 400

Returns:

  • (Integer, nil)

    the HTTP status



47
48
49
# File 'lib/simple_oauth/oauth2/error.rb', line 47

def status
  @status
end

#uriString? (readonly)

The URI of a page describing the error

Examples:

error.uri # => "https://example.com/errors/invalid_grant"

Returns:

  • (String, nil)

    the error URI



39
40
41
# File 'lib/simple_oauth/oauth2/error.rb', line 39

def uri
  @uri
end

Class Method Details

.from_response(status:, body:) ⇒ Error

Build the error described by an OAuth 2.0 error response

Examples:

SimpleOAuth::OAuth2::Error.from_response(status: 400, body: '{"error":"invalid_grant"}')

Parameters:

  • status (Integer, String)

    the HTTP status of the response

  • body (String, nil)

    the response body

  • status: (Integer, String)
  • body: (String, nil)

Returns:

Raises:

  • (ArgumentError)

    if the status is not an HTTP status



70
71
72
73
74
# File 'lib/simple_oauth/oauth2/error.rb', line 70

def self.from_response(status:, body:)
  params = ResponseBody.parse(body)
  new(code: params["error"], description: params["error_description"], uri: params["error_uri"],
    status: http_status(status))
end

.http_status(value) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The HTTP status of a response, as an Integer

Examples:

SimpleOAuth::OAuth2::Error.http_status("400") # => 400

Parameters:

  • value (Integer, String)

    the status of the response

Returns:

  • (Integer)

    the status

Raises:

  • (ArgumentError)

    if the value is not an HTTP status



57
58
59
# File 'lib/simple_oauth/oauth2/error.rb', line 57

def self.http_status(value)
  Integer(value, exception: false) || raise(ArgumentError, "#{INVALID_STATUS}: #{value.inspect}")
end