|
| 1 | +"use strict"; |
| 2 | +/* eslint-disable camelcase */ |
| 3 | +/* eslint-disable no-warning-comments */ |
| 4 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 5 | +exports.TemporarySessionExpiredError = exports.StripeUnknownError = exports.StripeInvalidGrantError = exports.StripeIdempotencyError = exports.StripeSignatureVerificationError = exports.StripeConnectionError = exports.StripeRateLimitError = exports.StripePermissionError = exports.StripeAuthenticationError = exports.StripeAPIError = exports.StripeInvalidRequestError = exports.StripeCardError = exports.StripeError = exports.generateV2Error = exports.generateV1Error = void 0; |
| 6 | +const generateV1Error = (rawStripeError) => { |
| 7 | + switch (rawStripeError.type) { |
| 8 | + case 'card_error': |
| 9 | + return new StripeCardError(rawStripeError); |
| 10 | + case 'invalid_request_error': |
| 11 | + return new StripeInvalidRequestError(rawStripeError); |
| 12 | + case 'api_error': |
| 13 | + return new StripeAPIError(rawStripeError); |
| 14 | + case 'authentication_error': |
| 15 | + return new StripeAuthenticationError(rawStripeError); |
| 16 | + case 'rate_limit_error': |
| 17 | + return new StripeRateLimitError(rawStripeError); |
| 18 | + case 'idempotency_error': |
| 19 | + return new StripeIdempotencyError(rawStripeError); |
| 20 | + case 'invalid_grant': |
| 21 | + return new StripeInvalidGrantError(rawStripeError); |
| 22 | + default: |
| 23 | + return new StripeUnknownError(rawStripeError); |
| 24 | + } |
| 25 | +}; |
| 26 | +exports.generateV1Error = generateV1Error; |
| 27 | +// eslint-disable-next-line complexity |
| 28 | +const generateV2Error = (rawStripeError) => { |
| 29 | + switch (rawStripeError.type) { |
| 30 | + // switchCases: The beginning of the section generated from our OpenAPI spec |
| 31 | + case 'temporary_session_expired': |
| 32 | + return new TemporarySessionExpiredError(rawStripeError); |
| 33 | + // switchCases: The end of the section generated from our OpenAPI spec |
| 34 | + } |
| 35 | + // Special handling for requests with missing required fields in V2 APIs. |
| 36 | + // invalid_field response in V2 APIs returns the field 'code' instead of 'type'. |
| 37 | + switch (rawStripeError.code) { |
| 38 | + case 'invalid_fields': |
| 39 | + return new StripeInvalidRequestError(rawStripeError); |
| 40 | + } |
| 41 | + return (0, exports.generateV1Error)(rawStripeError); |
| 42 | +}; |
| 43 | +exports.generateV2Error = generateV2Error; |
| 44 | +/** |
| 45 | + * StripeError is the base error from which all other more specific Stripe errors derive. |
| 46 | + * Specifically for errors returned from Stripe's REST API. |
| 47 | + */ |
| 48 | +class StripeError extends Error { |
| 49 | + constructor(raw = {}, type = null) { |
| 50 | + var _a; |
| 51 | + super(raw.message); |
| 52 | + this.type = type || this.constructor.name; |
| 53 | + this.raw = raw; |
| 54 | + this.rawType = raw.type; |
| 55 | + this.code = raw.code; |
| 56 | + this.doc_url = raw.doc_url; |
| 57 | + this.param = raw.param; |
| 58 | + this.detail = raw.detail; |
| 59 | + this.headers = raw.headers; |
| 60 | + this.requestId = raw.requestId; |
| 61 | + this.statusCode = raw.statusCode; |
| 62 | + this.message = (_a = raw.message) !== null && _a !== void 0 ? _a : ''; |
| 63 | + this.userMessage = raw.user_message; |
| 64 | + this.charge = raw.charge; |
| 65 | + this.decline_code = raw.decline_code; |
| 66 | + this.payment_intent = raw.payment_intent; |
| 67 | + this.payment_method = raw.payment_method; |
| 68 | + this.payment_method_type = raw.payment_method_type; |
| 69 | + this.setup_intent = raw.setup_intent; |
| 70 | + this.source = raw.source; |
| 71 | + } |
| 72 | +} |
| 73 | +exports.StripeError = StripeError; |
| 74 | +/** |
| 75 | + * Helper factory which takes raw stripe errors and outputs wrapping instances |
| 76 | + */ |
| 77 | +StripeError.generate = exports.generateV1Error; |
| 78 | +// Specific Stripe Error types: |
| 79 | +/** |
| 80 | + * CardError is raised when a user enters a card that can't be charged for |
| 81 | + * some reason. |
| 82 | + */ |
| 83 | +class StripeCardError extends StripeError { |
| 84 | + constructor(raw = {}) { |
| 85 | + super(raw, 'StripeCardError'); |
| 86 | + } |
| 87 | +} |
| 88 | +exports.StripeCardError = StripeCardError; |
| 89 | +/** |
| 90 | + * InvalidRequestError is raised when a request is initiated with invalid |
| 91 | + * parameters. |
| 92 | + */ |
| 93 | +class StripeInvalidRequestError extends StripeError { |
| 94 | + constructor(raw = {}) { |
| 95 | + super(raw, 'StripeInvalidRequestError'); |
| 96 | + } |
| 97 | +} |
| 98 | +exports.StripeInvalidRequestError = StripeInvalidRequestError; |
| 99 | +/** |
| 100 | + * APIError is a generic error that may be raised in cases where none of the |
| 101 | + * other named errors cover the problem. It could also be raised in the case |
| 102 | + * that a new error has been introduced in the API, but this version of the |
| 103 | + * Node.JS SDK doesn't know how to handle it. |
| 104 | + */ |
| 105 | +class StripeAPIError extends StripeError { |
| 106 | + constructor(raw = {}) { |
| 107 | + super(raw, 'StripeAPIError'); |
| 108 | + } |
| 109 | +} |
| 110 | +exports.StripeAPIError = StripeAPIError; |
| 111 | +/** |
| 112 | + * AuthenticationError is raised when invalid credentials are used to connect |
| 113 | + * to Stripe's servers. |
| 114 | + */ |
| 115 | +class StripeAuthenticationError extends StripeError { |
| 116 | + constructor(raw = {}) { |
| 117 | + super(raw, 'StripeAuthenticationError'); |
| 118 | + } |
| 119 | +} |
| 120 | +exports.StripeAuthenticationError = StripeAuthenticationError; |
| 121 | +/** |
| 122 | + * PermissionError is raised in cases where access was attempted on a resource |
| 123 | + * that wasn't allowed. |
| 124 | + */ |
| 125 | +class StripePermissionError extends StripeError { |
| 126 | + constructor(raw = {}) { |
| 127 | + super(raw, 'StripePermissionError'); |
| 128 | + } |
| 129 | +} |
| 130 | +exports.StripePermissionError = StripePermissionError; |
| 131 | +/** |
| 132 | + * RateLimitError is raised in cases where an account is putting too much load |
| 133 | + * on Stripe's API servers (usually by performing too many requests). Please |
| 134 | + * back off on request rate. |
| 135 | + */ |
| 136 | +class StripeRateLimitError extends StripeError { |
| 137 | + constructor(raw = {}) { |
| 138 | + super(raw, 'StripeRateLimitError'); |
| 139 | + } |
| 140 | +} |
| 141 | +exports.StripeRateLimitError = StripeRateLimitError; |
| 142 | +/** |
| 143 | + * StripeConnectionError is raised in the event that the SDK can't connect to |
| 144 | + * Stripe's servers. That can be for a variety of different reasons from a |
| 145 | + * downed network to a bad TLS certificate. |
| 146 | + */ |
| 147 | +class StripeConnectionError extends StripeError { |
| 148 | + constructor(raw = {}) { |
| 149 | + super(raw, 'StripeConnectionError'); |
| 150 | + } |
| 151 | +} |
| 152 | +exports.StripeConnectionError = StripeConnectionError; |
| 153 | +/** |
| 154 | + * SignatureVerificationError is raised when the signature verification for a |
| 155 | + * webhook fails |
| 156 | + */ |
| 157 | +class StripeSignatureVerificationError extends StripeError { |
| 158 | + constructor(header, payload, raw = {}) { |
| 159 | + super(raw, 'StripeSignatureVerificationError'); |
| 160 | + this.header = header; |
| 161 | + this.payload = payload; |
| 162 | + } |
| 163 | +} |
| 164 | +exports.StripeSignatureVerificationError = StripeSignatureVerificationError; |
| 165 | +/** |
| 166 | + * IdempotencyError is raised in cases where an idempotency key was used |
| 167 | + * improperly. |
| 168 | + */ |
| 169 | +class StripeIdempotencyError extends StripeError { |
| 170 | + constructor(raw = {}) { |
| 171 | + super(raw, 'StripeIdempotencyError'); |
| 172 | + } |
| 173 | +} |
| 174 | +exports.StripeIdempotencyError = StripeIdempotencyError; |
| 175 | +/** |
| 176 | + * InvalidGrantError is raised when a specified code doesn't exist, is |
| 177 | + * expired, has been used, or doesn't belong to you; a refresh token doesn't |
| 178 | + * exist, or doesn't belong to you; or if an API key's mode (live or test) |
| 179 | + * doesn't match the mode of a code or refresh token. |
| 180 | + */ |
| 181 | +class StripeInvalidGrantError extends StripeError { |
| 182 | + constructor(raw = {}) { |
| 183 | + super(raw, 'StripeInvalidGrantError'); |
| 184 | + } |
| 185 | +} |
| 186 | +exports.StripeInvalidGrantError = StripeInvalidGrantError; |
| 187 | +/** |
| 188 | + * Any other error from Stripe not specifically captured above |
| 189 | + */ |
| 190 | +class StripeUnknownError extends StripeError { |
| 191 | + constructor(raw = {}) { |
| 192 | + super(raw, 'StripeUnknownError'); |
| 193 | + } |
| 194 | +} |
| 195 | +exports.StripeUnknownError = StripeUnknownError; |
| 196 | +// classDefinitions: The beginning of the section generated from our OpenAPI spec |
| 197 | +class TemporarySessionExpiredError extends StripeError { |
| 198 | + constructor(rawStripeError = {}) { |
| 199 | + super(rawStripeError, 'TemporarySessionExpiredError'); |
| 200 | + } |
| 201 | +} |
| 202 | +exports.TemporarySessionExpiredError = TemporarySessionExpiredError; |
| 203 | +// classDefinitions: The end of the section generated from our OpenAPI spec |
0 commit comments