Errors and cancellation in .NET

The SponsorkitError exception and its codes, which methods throw what, and how each method reacts to a cancelled CancellationToken.

On this page

The .NET SDK follows the error contract every Sponsorkit SDK shares:

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

SponsorkitError#

Every exception the SDK throws on purpose is a SponsorkitError, a sealed subclass of Exception. Branch on its Code, a stable string, not on the message, which is for humans and may change.

MemberMeaning
CodeOne of the SponsorkitErrorCodes constants below.
MessageA readable description, fine to show or log.
RetryAfterTimeSpan?. For network_error caused by a rate limit: how long Sponsorkit asked you to wait. Invalid values are ignored and long ones are capped at 24 hours.
InnerExceptionThe underlying exception, such as an HttpRequestException, when there is one.

C# exception filters keep the handling tidy:

Program.cs
using Sponsorkit;

var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", AppName = "my-cli" });

try
{
    await sk.SignInAsync();
}
catch (SponsorkitError e) when (e.Code is SponsorkitErrorCodes.Cancelled or SponsorkitErrorCodes.Timeout)
{
    Console.Error.WriteLine("Sign-in didn't finish. Run `my-cli login` to try again.");
}
catch (SponsorkitError e) when (e.Code == SponsorkitErrorCodes.NetworkError)
{
    var hint = e.RetryAfter is { } wait ? $" Try again in {Math.Ceiling(wait.TotalMinutes)} minute(s)." : "";
    Console.Error.WriteLine($"Could not reach Sponsorkit.{hint}");
}
catch (SponsorkitError e)
{
    Console.Error.WriteLine($"Sign-in failed ({e.Code}): {e.Message}");
}

Error codes#

Code (SponsorkitErrorCodes.…)Thrown byWhen
invalid_config (InvalidConfig)constructor, CheckoutUrl, OpenCheckoutAsync, SignInAsync, SignOutAsync, NagOptions.ParseDuration, VerifySessionAsyncBad configuration or options; a provider that isn't one of your targets; a target that doesn't exist (dev-time check only); the state file couldn't be written while signing in or out.
disabled (Disabled)SignInAsync, OpenCheckoutAsyncSPONSORKIT_DISABLE is set.
access_denied (AccessDenied)SignInAsync, OpenCheckoutAsyncThe user declined at GitHub or Open Collective.
invalid_state (InvalidState)SignInAsync, OpenCheckoutAsyncThe sign-in state expired or couldn't be verified.
provider_error (ProviderError)SignInAsync, OpenCheckoutAsyncThe provider failed during sign-in.
invalid_code (InvalidCode)SignInAsync, OpenCheckoutAsyncSponsorkit rejected the code exchange, for example a mistyped or expired paste-back code.
cancelled (Cancelled)SignInAsync, OpenCheckoutAsyncThe sign-in's token was cancelled, or no paste-back code was entered.
timeout (Timeout)SignInAsync, OpenCheckoutAsyncThe sign-in didn't finish within SignInTimeout (5 minutes by default).
network_error (NetworkError)SignInAsync, OpenCheckoutAsync, VerifySessionAsyncSponsorkit was unreachable, timed out (RequestTimeout), or answered 5xx, 429, a 400 other than invalid_code, or something unexpected. A forced Loopback sign-in that can't listen on 127.0.0.1 also throws it.
session_invalid (SessionInvalid)VerifySessionAsyncThe token is malformed, expired or rejected.
targets_mismatch (TargetsMismatch)VerifySessionAsyncThe token was issued for different targets.
popup_blocked (PopupBlocked)neverBrowser-only; the constant exists for parity with the other SDKs.

OpenCheckoutAsync throws the sign-in codes only while it signs a signed-out user in. Each constant is documented in the SponsorkitErrorCodes reference.

Besides SponsorkitError, you'll only see the standard argument checks (ArgumentNullException for a null options object or predicate, ArgumentException for an empty nag id) and OperationCanceledException from cancellation, below.

Cancellation#

Every async method takes an optional CancellationToken as its last parameter. What cancelling does depends on what the method is for:

MethodWhen the token is cancelled
GetFinancialContributionAsyncThrows OperationCanceledException, the usual .NET behaviour.
SponsorkitNag.DueAsyncThrows OperationCanceledException (from the contribution lookup). The run is already counted.
SponsorkitServer.VerifySessionAsyncThrows OperationCanceledException.
SignInAsyncThrows SponsorkitError with code cancelled, so one catch handles "the user gave up" alongside other sign-in failures.
OpenCheckoutAsyncNever throws for cancellation. Returns CheckoutStatus.Unknown, in the sign-in phase and in the polling phase alike.
SignOutAsyncThrows OperationCanceledException only if the token is already cancelled when you call it; the call is otherwise synchronous.

Time limits work the same way. SignInTimeout makes a sign-in throw timeout, CheckoutOptions.Timeout makes checkout polling return Unknown, and RequestTimeout turns a slow request into a network error, which GetFinancialContributionAsync absorbs. To bound a whole call yourself, use a CancellationTokenSource with a delay:

Program.cs
using Sponsorkit;

var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat" });

// Don't let the startup check hold up the app for more than two seconds.
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
ContributionResult? fc = null;
try
{
    fc = await sk.GetFinancialContributionAsync(cts.Token);
}
catch (OperationCanceledException)
{
    // Carry on without it; the next start will most likely be served from the cache.
}
Console.WriteLine(fc?.SignedIn == true ? $"{fc.Active().Count} active contribution(s)" : "No answer yet");

Turning the SDK off#

With SPONSORKIT_DISABLE set, nothing touches the network or the disk: GetFinancialContributionAsync returns Disabled == true with no contributions, DueAsync returns false, SignOutAsync does nothing, CheckoutUrl still builds links, and SignInAsync and OpenCheckoutAsync throw disabled. Handle disabled wherever you call those two, for example by printing the checkout link instead. Handle errors and offline covers these situations across languages.