Skip to content

Commit

Permalink
Migrate Error, update import/export in StripeResource and autoPagination
Browse files Browse the repository at this point in the history
  • Loading branch information
anniel-stripe committed Sep 2, 2022
1 parent 796d947 commit aa07eb4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
92 changes: 65 additions & 27 deletions src/Error.js → src/Error.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
'use strict';

type RawErrorType =
| 'card_error'
| 'invalid_request_error'
| 'api_error'
| 'idempotency_error'
| 'rate_limit_error'
| 'authentication_error'
| 'invalid_grant';

type StripeRawError = {
message?: string;
type?: RawErrorType;

headers?: {[header: string]: string};
statusCode?: number;
requestId?: string;
code?: string;
doc_url?: string;
decline_code?: string;
param?: string;
detail?: string;
charge?: string;
payment_method_type?: string;

payment_intent?: any;
payment_method?: any;
setup_intent?: any;
source?: any;
exception?: any;
};

/**
* StripeError is the base error from which all other more specific Stripe errors derive.
* Specifically for errors returned from Stripe's REST API.
*/
class StripeError extends Error {
constructor(raw = {}) {
export class StripeError extends Error {
readonly message: string;
readonly type: string;
readonly raw: unknown;
readonly rawType: RawErrorType;
readonly headers: {[header: string]: string};
readonly requestId: string;

readonly code?: string;
readonly doc_url?: string;
readonly param?: string;
readonly detail?: string;
readonly statusCode?: number;
readonly charge?: string;
readonly decline_code?: string;
readonly payment_method_type?: string;

readonly payment_intent?: any;
readonly payment_method?: any;
readonly setup_intent?: any;
readonly source?: any;

constructor(raw: StripeRawError) {
super(raw.message);
this.type = this.constructor.name;

Expand Down Expand Up @@ -60,83 +112,69 @@ class StripeError extends Error {
* CardError is raised when a user enters a card that can't be charged for
* some reason.
*/
class StripeCardError extends StripeError {}
export class StripeCardError extends StripeError {}

/**
* InvalidRequestError is raised when a request is initiated with invalid
* parameters.
*/
class StripeInvalidRequestError extends StripeError {}
export class StripeInvalidRequestError extends StripeError {}

/**
* APIError is a generic error that may be raised in cases where none of the
* other named errors cover the problem. It could also be raised in the case
* that a new error has been introduced in the API, but this version of the
* Node.JS SDK doesn't know how to handle it.
*/
class StripeAPIError extends StripeError {}
export class StripeAPIError extends StripeError {}

/**
* AuthenticationError is raised when invalid credentials are used to connect
* to Stripe's servers.
*/
class StripeAuthenticationError extends StripeError {}
export class StripeAuthenticationError extends StripeError {}

/**
* PermissionError is raised in cases where access was attempted on a resource
* that wasn't allowed.
*/
class StripePermissionError extends StripeError {}
export class StripePermissionError extends StripeError {}

/**
* RateLimitError is raised in cases where an account is putting too much load
* on Stripe's API servers (usually by performing too many requests). Please
* back off on request rate.
*/
class StripeRateLimitError extends StripeError {}
export class StripeRateLimitError extends StripeError {}

/**
* StripeConnectionError is raised in the event that the SDK can't connect to
* Stripe's servers. That can be for a variety of different reasons from a
* downed network to a bad TLS certificate.
*/
class StripeConnectionError extends StripeError {}
export class StripeConnectionError extends StripeError {}

/**
* SignatureVerificationError is raised when the signature verification for a
* webhook fails
*/
class StripeSignatureVerificationError extends StripeError {}
export class StripeSignatureVerificationError extends StripeError {}

/**
* IdempotencyError is raised in cases where an idempotency key was used
* improperly.
*/
class StripeIdempotencyError extends StripeError {}
export class StripeIdempotencyError extends StripeError {}

/**
* InvalidGrantError is raised when a specified code doesn't exist, is
* expired, has been used, or doesn't belong to you; a refresh token doesn't
* exist, or doesn't belong to you; or if an API key's mode (live or test)
* doesn't match the mode of a code or refresh token.
*/
class StripeInvalidGrantError extends StripeError {}
export class StripeInvalidGrantError extends StripeError {}

/**
* Any other error from Stripe not specifically captured above
*/
class StripeUnknownError extends StripeError {}

module.exports.generate = StripeError.generate;
module.exports.StripeError = StripeError;
module.exports.StripeCardError = StripeCardError;
module.exports.StripeInvalidRequestError = StripeInvalidRequestError;
module.exports.StripeAPIError = StripeAPIError;
module.exports.StripeAuthenticationError = StripeAuthenticationError;
module.exports.StripePermissionError = StripePermissionError;
module.exports.StripeRateLimitError = StripeRateLimitError;
module.exports.StripeConnectionError = StripeConnectionError;
module.exports.StripeSignatureVerificationError = StripeSignatureVerificationError;
module.exports.StripeIdempotencyError = StripeIdempotencyError;
module.exports.StripeInvalidGrantError = StripeInvalidGrantError;
module.exports.StripeUnknownError = StripeUnknownError;
export class StripeUnknownError extends StripeError {}
2 changes: 1 addition & 1 deletion src/StripeResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,4 @@ StripeResource.prototype = {
},
};

export = StripeResource;
module.exports = StripeResource;
2 changes: 1 addition & 1 deletion src/autoPagination.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import * as utils from './utils';
import utils = require('./utils');
import makeRequest = require('./makeRequest');

export function makeAutoPaginationMethods(
Expand Down

0 comments on commit aa07eb4

Please sign in to comment.