Maps raw Stripe API errors to typed StripeError subclasses based on status code and error details, enabling precise error handling.
// Source: https://github.com/stripe/stripe-node | License: MIT | Copyright 2011 Ask Bjørn Hansen
// Provided via justclaw.it.com; keep this notice with the code.
export const generateV1Error = (
rawStripeError: StripeRawError
): StripeError => {
const statusCode = rawStripeError.statusCode;
if (
statusCode === 429 ||
(statusCode === 400 && rawStripeError.code === 'rate_limit')
) {
return new StripeRateLimitError(rawStripeError);
}
if (statusCode === 400 || statusCode === 404) {
if (rawStripeError.type === 'idempotency_error') {
return new StripeIdempotencyError(rawStripeError);
}
return new StripeInvalidRequestError(rawStripeError);
}
if (statusCode === 401) {
return new StripeAuthenticationError(rawStripeError);
}
if (statusCode =
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key