Billing SDK

Errors and retries

How the Faurya Billing SDK reports failures, which ones it retries for you, and how to handle the rest.

Errors and retries

Every error the SDK throws extends FauryaError, so one instanceof check catches them all. Errors that came back from the Faurya API are a FauryaApiError, with a more specific class for each kind of problem.

Handling an error

import {
  FauryaApiError,
  FauryaConflictError,
  FauryaUnprocessableError,
} from "@faurya/billing";

try {
  await faurya.billing.invoices.upsert("inv_1042", invoice);
} catch (error) {
  if (error instanceof FauryaConflictError && error.code === "INVOICE_LOCKED") {
    // The invoice was already paid, so it can't change.
    // Void it and send a corrected invoice with a new id.
  } else if (error instanceof FauryaUnprocessableError && error.code === "CUSTOMER_NOT_FOUND") {
    // Send the customer first, then retry the invoice.
  } else if (error instanceof FauryaApiError) {
    console.error(error.code, error.errors, error.requestId);
  }
  throw error;
}

Every FauryaApiError carries:

PropertyWhat it is
statusThe HTTP status, e.g. 409
codeA stable code to branch on, e.g. INVOICE_LOCKED
messageA human-readable explanation
errorsEvery field problem at once: { field, code, message }[]
requestIdQuote this to Faurya support — it links your logs to ours

Fix every field in one go

A validation failure lists every problem in the request, each with the exact path to the field, so you can fix them all at once:

catch (error) {
  if (error instanceof FauryaValidationError) {
    for (const e of error.errors) {
      console.log(`${e.field}: ${e.message}`);
      // line_items[2].amount: USD amounts have at most 2 decimal places
    }
  }
}

Error classes

ClassWhen you'll see itWhat to do
FauryaValidationError400 — a field is invalid or missing, or a payment id was already used (DUPLICATE_TRANSACTION)Fix the fields in errors
FauryaAuthenticationError401 — the API key is wrong or missingCheck FAURYA_API_KEY
FauryaNotFoundError404 — the object you asked for doesn't existCheck the id
FauryaConflictError409 — the change isn't allowed on what's already storedSee the codes below
FauryaPayloadTooLargeError413 — the request is over the size limitSend fewer lines or items per call
FauryaUnprocessableError422 — it refers to something Faurya doesn't have yetSend that object first
FauryaRateLimitError429 — too many requestsRetried automatically; for backlogs use bulk import
FauryaServerError5xx — a problem on Faurya's side; nothing was savedRetried automatically when safe
FauryaInputErrorCaught by the SDK before sending, e.g. an invalid idFix the call
FauryaConfigErrorMissing key, invalid option, or running in a browserFix your setup
FauryaTimeoutErrorThe request took longer than timeoutMsRetry, or raise the timeout
FauryaConnectionErrorFaurya couldn't be reachedCheck the network; retry
FauryaAbortErrorYou cancelled the request with an AbortSignal—

Codes you'll most likely meet

CodeMeaningFix
CUSTOMER_NOT_FOUNDThe invoice names a customer Faurya doesn't haveUpsert the customer first
PLAN_NOT_FOUNDA line names an unknown planUpsert the plan first
INVOICE_NOT_FOUNDThe transaction names an unknown invoiceUpsert the invoice first
INVOICE_LOCKEDThe invoice has a payment, refund or void, so it's frozenVoid it and send a new invoice
TRANSACTION_IMMUTABLEYou tried to change a transaction's amount or typeRecord a refund or reversal instead
EVENT_IMMUTABLEYou re-sent an event id with a different bodyRetract it and send a new event
PLAN_CADENCE_LOCKEDYou changed the interval of a plan already in useCreate a new plan
AMOUNT_EXCEEDS_INVOICEPayments would exceed the invoice totalCheck the amount
AMOUNT_EXCEEDS_PAYMENTA refund exceeds the payment it refundsCheck the amount
DUPLICATE_TRANSACTIONpayments.create with an id already recordedIt's already in Faurya — safe to ignore

The full list is in the Billing API errors reference.

What the SDK retries for you

You don't need to write retry loops. By default the SDK retries up to 2 times, waiting a little longer each time (starting at 0.5 s, capped at 8 s):

  • Rate limits (429) are retried for every call, waiting as long as Faurya asks (up to 60 s).
  • Timeouts, network errors and server errors are retried for every billing.* call and import call, because those are safe to repeat — sending the same object twice changes nothing.
  • payments.create, payments.delete and goals.create are never retried after a timeout or server error. The request may have landed, and retrying could record a payment twice. If you retry one yourself, the DUPLICATE_TRANSACTION check protects you.

Errors like 400, 409 and 422 are never retried — sending the same request again would fail the same way.

Change the retry count for the whole client, or for one call:

const faurya = createFauryaBilling({ maxRetries: 5 });

await faurya.billing.customers.upsert("cus_42", data, { maxRetries: 0 });

Timeouts and cancelling

Each request times out after 30 seconds by default. Override it per client or per call, or cancel with an AbortSignal:

// Slower network? Give it more time.
await faurya.billing.invoices.upsert("inv_1042", invoice, { timeoutMs: 60_000 });

// Cancel if the user leaves
const controller = new AbortController();
const pending = faurya.billing.subscriptions.retrieve("sub_900", { signal: controller.signal });
controller.abort(); // throws FauryaAbortError

Log the request id

Always log requestId with failures. It's the one value Faurya support needs to find your request:

catch (error) {
  if (error instanceof FauryaApiError) {
    logger.error({ code: error.code, status: error.status, requestId: error.requestId });
  }
}

Successful responses carry it too, as request_id.

Warnings are not errors

A successful response can include warnings. They never mean the save failed — just that something is worth knowing:

WarningMeaning
VISITOR_PENDINGThe visitor hasn't been processed yet. The link completes on its own.
VISITOR_ALREADY_LINKEDThat visitor already belongs to another customer, so the link wasn't moved. The object was still saved.
IGNORED_FIELDA compatibility field Faurya accepts but doesn't store.
const res = await faurya.billing.customers.upsert("cus_42", { faurya_visitor_id: visitorId });
for (const w of res.warnings) console.warn(w.code, w.message);