Handle errors and offline

Which calls can fail and with which codes, why reading contributions never throws, and how stale data, Retry-After and expired sign-ins are reported.

On this page

Sponsorkit is built so that adding it can't break your app. Reading contributions never fails because of the network, Sponsorkit or a provider. The calls where a person is waiting for an answer (sign-in, checkout, backend verification) do report errors, with a stable code that's the same in every SDK.

Reading contributions never throws#

getFinancialContribution() doesn't throw for network errors, timeouts, server errors or rate limits. Instead:

SituationResult
Not signed inSigned-out result (signedIn: false). No network call.
Cache younger than cacheTtlThe cached result, fromCache: true. No network call.
Fresh answer from the APIThe live result, fromCache: false. It replaces the cache.
Network error, timeout, 5xx or 429The cached result with fromCache: true and stale: true. With no cache yet: a signed-in result with no contributions, supporter: null and stale: true.
One provider unreachableLive data from the other provider, plus the cached contributions of the unreachable one; stale: true.
Session rejected (session_invalid)The stored token and cache are cleared; signed-out result.
SPONSORKIT_DISABLE setSigned-out result with disabled: true. No network or disk.

The one exception is configuration: a malformed target fails with invalid_config when you construct the client, and when dev checks are on, a target that doesn't exist fails the first call with invalid_config. Both are bugs to fix during development, never something that happens to your users.

In .NET, cancelling the CancellationToken you pass to GetFinancialContributionAsync throws the usual OperationCanceledException.

Showing stale data#

stale: true means "this may be out of date": at least part of the result couldn't be refreshed. fromCache tells you the whole result came from the local cache, and fetchedAt is when the data was fetched from the API (null if never). Most apps can ignore all three and use the result as is. If you show sponsorship status prominently, you can mention it:

status.ts
import { Sponsorkit } from "@sponsorkit/sdk/node";

const sk = new Sponsorkit({ github: "octocat", openCollective: "my-project", appName: "my-cli" });

const fc = await sk.getFinancialContribution();
if (!fc.signedIn) {
  console.log("Not signed in. Run `my-cli login` if you sponsor my-cli.");
} else {
  console.log(`Active sponsorships: ${fc.active().length}`);
  if (fc.stale) console.log(`(Couldn't reach Sponsorkit. Last update: ${fc.fetchedAt ?? "never"}.)`);
  for (const identity of fc.supporter?.identities ?? []) {
    if (identity.needsReconnect) console.log(`Please sign in to ${identity.provider} again: my-cli login`);
  }
}

Expired provider sign-ins#

An Open Collective sign-in expires after 90 days, and a person can revoke Sponsorkit's access on GitHub or Open Collective at any time. Sponsorkit then marks that identity needsReconnect: true, lists its provider in unavailable with stale: true, and the SDK keeps serving that provider's cached contributions. The rest of the session keeps working. To fix it, call signIn({ provider }) with that provider: it replaces the expired identity in the same session.

Rate limits and Retry-After#

The API allows one refresh per minute per session. A request inside that window gets 429 with a Retry-After header, and the SDK:

  • serves the cache, as for any network problem;
  • remembers the back-off for the rest of the process, so later calls don't even try until it has passed, even with cacheTtl: 0;
  • ignores values it can't use and caps the rest at 24 hours, so a broken header can't stall your app.

With the default cacheTtl of one hour you'll rarely see this. It mostly matters if you refresh with cacheTtl: 0 in a loop.

Timeouts#

Every HTTP request has a 30-second timeout. Change it with requestTimeout (seconds in JavaScript and Python, RequestTimeout as a TimeSpan/time.Duration in .NET and Go, request_timeout as a Duration in Rust). 0 disables it, except in Go, where the zero value means the default and sponsorkit.NoRequestTimeout disables it. A timeout counts as a network error: reading serves the cache, and sign-in or verification fails with network_error.

Errors that are thrown#

Every error the SDKs throw is a SponsorkitError (*sponsorkit.Error in Go, sponsorkit::Error in Rust) with a code:

CodeThrown byMeaning and what to do
invalid_configconstructor, any callMissing or malformed targets or options, an unknown mock preset, a target that doesn't exist (dev check), or storage that can't be written when signing in or out. Fix the configuration.
disabledsignIn, openCheckoutSPONSORKIT_DISABLE is set. Hide or disable your sign-in UI.
popup_blockedsignIn, openCheckout (browser)The popup was blocked and there's no redirect fallback.
access_deniedsignInThe person declined at GitHub or Open Collective. Nothing to report.
invalid_statesignInThe sign-in took too long at the provider (over 10 minutes). Try again.
provider_errorsignInThe provider failed during sign-in. Try again later.
invalid_codesignInThe sign-in code was rejected: expired, mistyped when pasted, or from another attempt. Try again.
cancelledsignInThe person closed the popup, entered no code, or you cancelled.
timeoutsignIn, openCheckoutSign-in took longer than signInTimeout (300 s).
network_errorsignIn, openCheckout, verifySessionSponsorkit couldn't be reached, timed out, or answered 5xx, 429 or something unexpected. Has retryAfter (seconds) when the API sent Retry-After. Retry later.
session_invalidverifySessionThe token is missing, expired, tampered with or otherwise invalid.
targets_mismatchverifySessionThe token was issued for different targets.

The codes and their meanings are part of the wire protocol, so they're identical across languages. The field is code in JavaScript and Python, Code in .NET and Go, and code() (an ErrorCode enum, with as_str() for the string) in Rust. In Go, sponsorkit.ErrorCodeOf(err) returns the code of any error, or "" for nil.

openCheckout() follows the same rules for its sign-in phase, but cancellation and the polling timeout resolve { status: "unknown" } instead of throwing. See checkout.

The error contract page explains the design behind these rules.