Errors and cancellation in Rust

The sponsorkit::Error type and its ErrorCode values, which methods return what, and how to cancel sign-in and checkout with a CancellationToken or by dropping the future.

On this page

The Rust SDK follows the error contract every Sponsorkit SDK shares:

  • Reading never breaks your app. get_financial_contribution doesn't return an error for network errors, outages, 5xx responses or rate limits. It reports them through stale (see contributions).
  • Configuration mistakes fail fast, in Sponsorkit::new or at the call with the bad options.
  • Actions the user asked for (sign-in, checkout) return an error when they can't complete, so you can tell the user why.

sponsorkit::Error#

Every fallible function returns sponsorkit::Result<T>, an alias for std::result::Result<T, sponsorkit::Error>. Error is a small struct; branch on its code(), not on the message, which is for humans and may change.

MethodReturnsMeaning
code()ErrorCodeOne of the codes below.
message()&strA readable description, fine to show or log.
retry_after()Option<Duration>For network_error caused by a rate limit: how long Sponsorkit asked you to wait (its Retry-After, whole seconds, capped at 24 hours).

Error implements std::error::Error, Display, Debug and Clone, and is Send + Sync + 'static, so ? converts it into Box<dyn std::error::Error> or anyhow::Error. It displays as sponsorkit: <message> (<code>):

sponsorkit: amount must be a positive number of minor units, got 0 (invalid_config)

Error::new(code, message) is public, for code (such as test doubles) that wants to produce the same errors.

Error codes#

ErrorCode is a Copy enum. code.as_str() (or Display) gives the wire spelling shared by all SDKs. It's #[non_exhaustive], so a match on it needs a _ arm.

ErrorCodeWire valueReturned byWhen
InvalidConfiginvalid_confignew, any call with bad options; sign_in and sign_outMissing or malformed targets, an unknown mock preset, invalid checkout options, an unconfigured provider, the dev-time check found a missing target, or the state file couldn't be written.
Disableddisabledsign_in, open_checkoutSPONSORKIT_DISABLE is set.
PopupBlockedpopup_blockedneverOnly the browser SDK produces it; listed for parity.
AccessDeniedaccess_deniedsign_in, open_checkoutThe user declined on the provider's consent screen.
InvalidStateinvalid_statesign_in, open_checkoutThe OAuth state expired or couldn't be verified.
ProviderErrorprovider_errorsign_in, open_checkoutThe provider failed during sign-in.
InvalidCodeinvalid_codesign_in, open_checkoutThe API rejected the sign-in code, for example a mistyped paste-back code.
Cancelledcancelledsign_in, open_checkoutSignInOptions::cancel fired, or nothing was entered at the paste-back prompt (also while open_checkout signs in).
Timeouttimeoutsign_in, open_checkoutSigning in took longer than sign_in_timeout.
NetworkErrornetwork_errorsign_in, open_checkout, verify_sessionThe API couldn't be reached, timed out, or answered 5xx, 429 or something unexpected.
SessionInvalidsession_invalidverify_sessionThe session token is invalid or expired.
TargetsMismatchtargets_mismatchverify_sessionThe session was issued for different targets.

What each method can return:

MethodErrors
Sponsorkit::newinvalid_config only
get_financial_contributioninvalid_config from the dev-time check only; never network errors
sign_ineverything in the table except popup_blocked, session_invalid and targets_mismatch
open_checkoutlike sign_in; but CheckoutOptions::cancel and the polling timeout give Ok(CheckoutOutcome::Unknown) instead of an error
checkout_urlinvalid_config only
sign_outinvalid_config, if the state file can't be written
Nag::due, Nag::shownnone: due returns bool, and errors read as false
server::verify_sessioninvalid_config, session_invalid, targets_mismatch, network_error
parse_iso_duration, Target::parse, "…".parse::<MockPreset>()invalid_config

Handling errors#

src/main.rs
use sponsorkit::{ErrorCode, Options, SignInOptions, Sponsorkit};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sk = Sponsorkit::new(Options { github: Some("octocat".into()), ..Default::default() })?;

    if let Err(e) = sk.sign_in(SignInOptions::default()).await {
        match e.code() {
            ErrorCode::AccessDenied | ErrorCode::Cancelled => eprintln!("No problem, maybe later."),
            ErrorCode::Timeout => eprintln!("Sign-in timed out. Try again with `my-cli login`."),
            ErrorCode::NetworkError => match e.retry_after() {
                Some(wait) => eprintln!("Sponsorkit is busy; try again in {} s.", wait.as_secs()),
                None => eprintln!("Couldn't reach Sponsorkit. Check your connection."),
            },
            ErrorCode::Disabled => {} // the user turned Sponsorkit off: stay quiet
            _ => return Err(e.into()), // invalid_config and the rest: a bug to fix
        }
    }
    Ok(())
}

Rate limits and Retry-After#

When the API answers 429, it sends Retry-After in seconds. The SDK handles it differently depending on who's waiting:

  • get_financial_contribution serves the cache and doesn't ask again for that long (60 seconds when the header is missing or invalid), for the rest of the process.
  • sign_in, open_checkout and verify_session fail with network_error, and e.retry_after() tells you how long to wait.

Values that aren't a non-negative whole number of seconds are ignored; values over 24 hours are capped at MAX_RETRY_AFTER.

Cancellation#

Rust futures do nothing unless polled, so there are two ways to stop an SDK call:

  1. A CancellationToken in SignInOptions::cancel or CheckoutOptions::cancel. The crate re-exports tokio_util::sync::CancellationToken as sponsorkit::CancellationToken, so you don't need tokio-util in your own dependencies. Cancelling gives you a result:

    MethodOn cancel
    sign_inErr with ErrorCode::Cancelled
    open_checkoutOk(CheckoutOutcome::Unknown), in the sign-in phase or the polling phase
  2. Dropping the future, for example with tokio::time::timeout, a tokio::select! branch that loses, or aborting the task. Everything stops, including the loopback listener, but there's no result. This works for every async method, including get_financial_contribution and verify_session, which don't take a token.

src/main.rs
use std::time::Duration;

use sponsorkit::{Options, SignInOptions, Sponsorkit};

#[tokio::main]
async fn main() -> Result<(), sponsorkit::Error> {
    let sk = Sponsorkit::new(Options { github: Some("octocat".into()), ..Default::default() })?;

    // Dropping the future stops the sign-in (and its loopback listener) without a result.
    match tokio::time::timeout(Duration::from_secs(60), sk.sign_in(SignInOptions::default())).await {
        Ok(result) => {
            result?;
            println!("Signed in.");
        }
        Err(_elapsed) => println!("Gave up after a minute."),
    }
    Ok(())
}

A token is the better choice for Ctrl+C handling, because the user gets a clear cancelled result; see the sign-in and checkout examples. A cancelled or timed-out paste-back prompt leaves the SDK's stdin reader waiting for one more line; see paste-back and stdin.

Turning Sponsorkit off#

Users can switch the SDK off with SPONSORKIT_DISABLE=1. Then nothing touches the network or the disk:

  • get_financial_contribution returns disabled: true with no contributions;
  • nags are never due;
  • sign_in and open_checkout return disabled, so a login or sponsor command can say "Sponsorkit is turned off" instead of hanging;
  • sign_out does nothing, checkout_url still returns a link, and is_disabled() tells you the state.

Targets are still validated, so a configuration mistake doesn't hide behind the switch.