@usethrottle/errors
v1.0.0
Published
Shared error class thrown by every Throttle SDK. Catch one ThrottleError across @usethrottle/cart, @usethrottle/checkout-sdk, @usethrottle/discounts and @usethrottle/subscriptions.
Readme
@usethrottle/errors
The shared error class thrown by every Throttle SDK.
Each Throttle SDK used to ship its own structurally-identical error class
(ThrottleApiError, ThrottleCheckoutError, ThrottleDiscountsError,
ThrottleSubscriptionsError). If you used two of them you had to catch two
names. Now every SDK error extends a single ThrottleError, so one check
covers all of them. The original class names still work — they are subclasses
of ThrottleError, kept for backward compatibility.
Install
You normally don't install this directly — it comes in as a dependency of the
SDK you use, and each SDK re-exports ThrottleError. Install it explicitly only
if you want to reference the shared type without depending on a specific SDK:
npm install @usethrottle/errorsUsage
import { ThrottleError } from '@usethrottle/errors';
// or, equivalently, from any SDK:
// import { ThrottleError } from '@usethrottle/cart';
// import { ThrottleError } from '@usethrottle/checkout-sdk';
try {
const cart = await cartClient.carts.create({ applicationId });
await checkoutClient.completeSession(sessionId, payment);
} catch (e) {
if (e instanceof ThrottleError) {
// One catch for every Throttle SDK.
console.error(`[${e.statusCode}] ${e.code}: ${e.message}`, e.details);
} else {
throw e;
}
}isThrottleError(value) is a convenience type guard for the same check.
Shape
class ThrottleError extends Error {
readonly code: string; // machine-readable error code from the API envelope
readonly statusCode: number; // HTTP status of the failed response
readonly details?: unknown; // per-field detail when the API provides it
readonly body?: unknown; // full parsed response body (when an SDK attaches it)
}Backward compatibility
The per-SDK names are unchanged and still throwable / catchable:
| Package | Class name (still exported) |
| --------------------------- | --------------------------------------- |
| @usethrottle/cart | ThrottleApiError extends ThrottleError |
| @usethrottle/checkout-sdk | ThrottleCheckoutError extends ThrottleError |
| @usethrottle/discounts | ThrottleDiscountsError extends ThrottleError |
| @usethrottle/subscriptions| ThrottleSubscriptionsError extends ThrottleError |
err instanceof ThrottleApiError keeps working; err instanceof ThrottleError
now also works for all of them.
