Exception: X::TokenReportFailed

Inherits:
Error
  • Object
show all
Defined in:
x-core/lib/x/core/errors/token_report_failed.rb

Overview

Error raised when save_tokens raised for the tokens of an exchange of a code or of a refresh

An authorization code works once, and so does a refresh token, so the tokens X issues for either are held in memory alone until save_tokens stores them: the first refresh token of X::OAuth2Authorization#client by the client it builds, and the one a refresh issued by the authenticator that refreshed, since the one it replaced is spent. They are not lost to a failure to store them, such as a database that is briefly down: the error holds them, so they can be stored again, and the client, which the exchange builds, or whose request refreshed. The error save_tokens raised is the cause, whose message the message ends with.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, client: nil, tokens: nil) ⇒ TokenReportFailed

Initialize the error with the client and the tokens it failed to report

Examples:

Raise the error for tokens save_tokens raised for

raise X::TokenReportFailed.new(client:, tokens:)

Parameters:

  • message (String, nil) (defaults to: nil) —

    the message, or nil for one that says the tokens of an exchange were not stored

  • client (Client, nil) (defaults to: nil) —

    the client the authorization built, or whose request refreshed the tokens

  • tokens (OAuth2Tokens, nil) (defaults to: nil) —

    the tokens of the exchange or the refresh



43
44
45
46
47
# File 'x-core/lib/x/core/errors/token_report_failed.rb', line 43

def initialize(message = nil, client: nil, tokens: nil)
  @client = client
  @tokens = tokens
  super(message || "The code was exchanged for tokens, but save_tokens raised for them")
end

Instance Attribute Details

#client ⇒ Client? (readonly)

The client the authorization built, or whose request refreshed the tokens

Examples:

Act for the user once the tokens are stored

rescue X::TokenReportFailed => e
  store(e.tokens.refresh_token)
  e.client.get("users/me")

Returns:

  • (Client, nil) —

    the client, or nil for a refresh made by an authenticator alone, as its refresh! makes one



24
25
26
# File 'x-core/lib/x/core/errors/token_report_failed.rb', line 24

def client
  @client
end

#tokens ⇒ OAuth2Tokens? (readonly)

The tokens of the exchange or the refresh, which save_tokens raised for

Examples:

Store the tokens again

rescue X::TokenReportFailed => e
  store(e.tokens.refresh_token)

Returns:

  • (OAuth2Tokens, nil) —

    the tokens, or nil if none were given



32
33
34
# File 'x-core/lib/x/core/errors/token_report_failed.rb', line 32

def tokens
  @tokens
end

Instance Method Details

#to_s ⇒ String

The message, ending with why save_tokens raised

It ends with the message of the error save_tokens raised, which is the cause, if there is one.

Examples:

Read why the tokens were not stored

error.message # => "The code was exchanged for tokens, but save_tokens raised for them: connection refused"

Returns:

  • (String) —

    the message



57
# File 'x-core/lib/x/core/errors/token_report_failed.rb', line 57

def to_s = [super, cause&.message].compact.join(": ")