Exception: Safire::Errors::OAuthError Abstract

Inherits:
Error
  • Object
show all
Defined in:
lib/safire/errors.rb

Overview

This class is abstract.

Base class for OAuth 2.0 protocol errors returned by the authorization server.

Provides a shared structure for errors that originate from HTTP interactions with OAuth 2.0 endpoints (token, authorization, registration). Subclasses define #operation_label to set the lead phrase of the error message and may override #build_message to handle structural failure paths (e.g. a valid HTTP response that is missing a required field such as access_token or client_id).

Can be used as a single rescue point for any server-side OAuth protocol failure:

rescue Safire::Errors::OAuthError => e

Direct Known Subclasses

AuthError, RegistrationError, TokenError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: nil, error_code: nil, error_description: nil) ⇒ OAuthError

Returns a new instance of OAuthError.



133
134
135
136
137
138
# File 'lib/safire/errors.rb', line 133

def initialize(status: nil, error_code: nil, error_description: nil)
  @status            = status
  @error_code        = error_code
  @error_description = error_description
  super(build_message)
end

Instance Attribute Details

#error_codeString? (readonly)

Returns OAuth2 error field (e.g. “invalid_grant”).

Returns:

  • (String, nil)

    OAuth2 error field (e.g. “invalid_grant”)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/safire/errors.rb', line 130

class OAuthError < Error
  attr_reader :status, :error_code, :error_description

  def initialize(status: nil, error_code: nil, error_description: nil)
    @status            = status
    @error_code        = error_code
    @error_description = error_description
    super(build_message)
  end

  private

  # @abstract Subclasses must define this to set the lead phrase of the error message.
  def operation_label
    raise NotImplementedError, "#{self.class} must define #operation_label"
  end

  def build_message
    parts = [operation_label]
    parts << "HTTP #{@status}" if @status
    parts << @error_code if @error_code
    parts << @error_description if @error_description
    parts.join('')
  end
end

#error_descriptionString? (readonly)

Returns OAuth2 error_description field.

Returns:

  • (String, nil)

    OAuth2 error_description field



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/safire/errors.rb', line 130

class OAuthError < Error
  attr_reader :status, :error_code, :error_description

  def initialize(status: nil, error_code: nil, error_description: nil)
    @status            = status
    @error_code        = error_code
    @error_description = error_description
    super(build_message)
  end

  private

  # @abstract Subclasses must define this to set the lead phrase of the error message.
  def operation_label
    raise NotImplementedError, "#{self.class} must define #operation_label"
  end

  def build_message
    parts = [operation_label]
    parts << "HTTP #{@status}" if @status
    parts << @error_code if @error_code
    parts << @error_description if @error_description
    parts.join('')
  end
end

#statusInteger? (readonly)

Returns HTTP status code.

Returns:

  • (Integer, nil)

    HTTP status code



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/safire/errors.rb', line 130

class OAuthError < Error
  attr_reader :status, :error_code, :error_description

  def initialize(status: nil, error_code: nil, error_description: nil)
    @status            = status
    @error_code        = error_code
    @error_description = error_description
    super(build_message)
  end

  private

  # @abstract Subclasses must define this to set the lead phrase of the error message.
  def operation_label
    raise NotImplementedError, "#{self.class} must define #operation_label"
  end

  def build_message
    parts = [operation_label]
    parts << "HTTP #{@status}" if @status
    parts << @error_code if @error_code
    parts << @error_description if @error_description
    parts.join('')
  end
end