UNPKG

4.58 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * StripeError is the base error from which all other more specific Stripe errors derive.
5 * Specifically for errors returned from Stripe's REST API.
6 */
7class StripeError extends Error {
8 constructor(raw = {}) {
9 super(raw.message);
10 this.type = this.constructor.name;
11
12 this.raw = raw;
13 this.rawType = raw.type;
14 this.code = raw.code;
15 this.doc_url = raw.doc_url;
16 this.param = raw.param;
17 this.detail = raw.detail;
18 this.headers = raw.headers;
19 this.requestId = raw.requestId;
20 this.statusCode = raw.statusCode;
21 this.message = raw.message;
22
23 this.charge = raw.charge;
24 this.decline_code = raw.decline_code;
25 this.payment_intent = raw.payment_intent;
26 this.payment_method = raw.payment_method;
27 this.setup_intent = raw.setup_intent;
28 this.source = raw.source;
29 }
30
31 /**
32 * Helper factory which takes raw stripe errors and outputs wrapping instances
33 */
34 static generate(rawStripeError) {
35 switch (rawStripeError.type) {
36 case 'card_error':
37 return new StripeCardError(rawStripeError);
38 case 'invalid_request_error':
39 return new StripeInvalidRequestError(rawStripeError);
40 case 'api_error':
41 return new StripeAPIError(rawStripeError);
42 case 'authentication_error':
43 return new StripeAuthenticationError(rawStripeError);
44 case 'rate_limit_error':
45 return new StripeRateLimitError(rawStripeError);
46 case 'idempotency_error':
47 return new StripeIdempotencyError(rawStripeError);
48 case 'invalid_grant':
49 return new StripeInvalidGrantError(rawStripeError);
50 default:
51 return new GenericError('Generic', 'Unknown Error');
52 }
53 }
54}
55
56// Specific Stripe Error types:
57
58/**
59 * CardError is raised when a user enters a card that can't be charged for
60 * some reason.
61 */
62class StripeCardError extends StripeError {}
63
64/**
65 * InvalidRequestError is raised when a request is initiated with invalid
66 * parameters.
67 */
68class StripeInvalidRequestError extends StripeError {}
69
70/**
71 * APIError is a generic error that may be raised in cases where none of the
72 * other named errors cover the problem. It could also be raised in the case
73 * that a new error has been introduced in the API, but this version of the
74 * Node.JS SDK doesn't know how to handle it.
75 */
76class StripeAPIError extends StripeError {}
77
78/**
79 * AuthenticationError is raised when invalid credentials are used to connect
80 * to Stripe's servers.
81 */
82class StripeAuthenticationError extends StripeError {}
83
84/**
85 * PermissionError is raised in cases where access was attempted on a resource
86 * that wasn't allowed.
87 */
88class StripePermissionError extends StripeError {}
89
90/**
91 * RateLimitError is raised in cases where an account is putting too much load
92 * on Stripe's API servers (usually by performing too many requests). Please
93 * back off on request rate.
94 */
95class StripeRateLimitError extends StripeError {}
96
97/**
98 * StripeConnectionError is raised in the event that the SDK can't connect to
99 * Stripe's servers. That can be for a variety of different reasons from a
100 * downed network to a bad TLS certificate.
101 */
102class StripeConnectionError extends StripeError {}
103
104/**
105 * SignatureVerificationError is raised when the signature verification for a
106 * webhook fails
107 */
108class StripeSignatureVerificationError extends StripeError {}
109
110/**
111 * IdempotencyError is raised in cases where an idempotency key was used
112 * improperly.
113 */
114class StripeIdempotencyError extends StripeError {}
115
116/**
117 * InvalidGrantError is raised when a specified code doesn't exist, is
118 * expired, has been used, or doesn't belong to you; a refresh token doesn't
119 * exist, or doesn't belong to you; or if an API key's mode (live or test)
120 * doesn't match the mode of a code or refresh token.
121 */
122class StripeInvalidGrantError extends StripeError {}
123
124module.exports.generate = StripeError.generate;
125module.exports.StripeError = StripeError;
126module.exports.StripeCardError = StripeCardError;
127module.exports.StripeInvalidRequestError = StripeInvalidRequestError;
128module.exports.StripeAPIError = StripeAPIError;
129module.exports.StripeAuthenticationError = StripeAuthenticationError;
130module.exports.StripePermissionError = StripePermissionError;
131module.exports.StripeRateLimitError = StripeRateLimitError;
132module.exports.StripeConnectionError = StripeConnectionError;
133module.exports.StripeSignatureVerificationError = StripeSignatureVerificationError;
134module.exports.StripeIdempotencyError = StripeIdempotencyError;
135module.exports.StripeInvalidGrantError = StripeInvalidGrantError;