Maps raw Stripe API errors to typed StripeError subclasses based on status code and error details, enabling precise error handling.
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 === 402) {
return new StripeCardError(rawStripeError);
}
if (statusCode === 403) {
return new StripePermissionError(rawStripeError);
}
return new
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key