-
Notifications
You must be signed in to change notification settings - Fork 773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor errors to ES6 classes #661
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
82be852
adjust ESLint config removing unused/unneeded plugins and fixing Pret…
tinovyatkin 784caec
refactore errors to be ES6 classes
tinovyatkin 2c5a837
porting errors descriptions from stripe-ruby
tinovyatkin 126447c
remove conflicting rule
tinovyatkin f3528b4
shim private methods
tinovyatkin 2ae978f
fix GenericError at `generate`
tinovyatkin 2bbe046
reverted GenericError constructor
tinovyatkin 7ffa99d
it's `Object.assign`!
tinovyatkin 4261fc9
add @rattrayalex-stripe deprecation comment
tinovyatkin 62d2748
restore original test
tinovyatkin ca4e1a4
make GenericError default export
tinovyatkin 841587a
and GenericError at `generate`
tinovyatkin 42e2e8a
handling edge cases as standard Errors
tinovyatkin 1b51929
my last attempt here
tinovyatkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,197 @@ | ||
'use strict'; | ||
|
||
const utils = require('./utils'); | ||
|
||
module.exports = _Error; | ||
|
||
/** | ||
* Generic Error klass to wrap any errors returned by stripe-node | ||
* @typedef {{ message?: string, type?: string, code?: number, param?: string, detail?: string, headers?: Record<string, string>, requestId?: string, statusCode?: number }} ErrorParams | ||
*/ | ||
function _Error(raw) { | ||
this.populate(...arguments); | ||
this.stack = new Error(this.message).stack; | ||
} | ||
/** | ||
* Generic Error class to wrap any errors returned by stripe-node | ||
*/ | ||
class GenericError extends Error { | ||
/** | ||
* | ||
* @param {string} type | ||
* @param {string} message | ||
*/ | ||
constructor(type, message) { | ||
super(message); | ||
this.type = type || this.constructor.name; | ||
// Saving class name in the property of our custom error as a shortcut. | ||
this.name = this.constructor.name; | ||
|
||
// Extend Native Error | ||
_Error.prototype = Object.create(Error.prototype); | ||
// Capturing stack trace, excluding constructor call from it. | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
|
||
_Error.prototype.type = 'GenericError'; | ||
_Error.prototype.populate = function(type, message) { | ||
this.type = type; | ||
this.message = message; | ||
}; | ||
/** | ||
* | ||
* @param {string} [type] - error type name | ||
* @param {string} [message] | ||
*/ | ||
populate(type, message) { | ||
this.type = type; | ||
this.message = message; | ||
} | ||
|
||
_Error.extend = utils.protoExtend; | ||
/** | ||
* DEPRECATED | ||
* Please use ES6 class inheritance instead. | ||
* @param {{ type: string, message?: string, [k:string]: any }} options | ||
*/ | ||
static extend(options) { | ||
class customError extends StripeError { | ||
/** | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Function_names_in_classes} | ||
*/ | ||
// @ts-ignore | ||
static get name() { | ||
return options.type; | ||
} | ||
} | ||
Object.assign(customError.prototype, options); | ||
return customError; | ||
} | ||
} | ||
|
||
/** | ||
* Create subclass of internal Error klass | ||
* StripeError is the base error from which all other more specific Stripe | ||
* errors derive. | ||
* (Specifically for errors returned from Stripe's REST API) | ||
* | ||
*/ | ||
const StripeError = (_Error.StripeError = _Error.extend({ | ||
type: 'StripeError', | ||
class StripeError extends GenericError { | ||
/** | ||
* | ||
* @param {ErrorParams} [raw] | ||
*/ | ||
constructor(raw = {}) { | ||
super(undefined, raw.message); | ||
this.populate(raw); | ||
} | ||
/** | ||
* | ||
* @param {ErrorParams} raw | ||
*/ | ||
// @ts-ignore | ||
populate(raw) { | ||
// Move from prototype def (so it appears in stringified obj) | ||
this.type = this.type; | ||
|
||
this.stack = new Error(raw.message).stack; | ||
if (!raw || typeof raw !== 'object' || Object.keys(raw).length < 1) { | ||
return; | ||
} | ||
this.raw = raw; | ||
this.rawType = raw.type; | ||
this.code = raw.code; | ||
this.param = raw.param; | ||
this.message = raw.message; | ||
this.detail = raw.detail; | ||
this.raw = raw; | ||
this.headers = raw.headers; | ||
this.requestId = raw.requestId; | ||
this.statusCode = raw.statusCode; | ||
}, | ||
})); | ||
this.message = raw.message; | ||
} | ||
|
||
/** | ||
* Helper factory which takes raw stripe errors and outputs wrapping instances | ||
*/ | ||
StripeError.generate = (rawStripeError) => { | ||
switch (rawStripeError.type) { | ||
case 'card_error': | ||
return new _Error.StripeCardError(rawStripeError); | ||
case 'invalid_request_error': | ||
return new _Error.StripeInvalidRequestError(rawStripeError); | ||
case 'api_error': | ||
return new _Error.StripeAPIError(rawStripeError); | ||
case 'idempotency_error': | ||
return new _Error.StripeIdempotencyError(rawStripeError); | ||
case 'invalid_grant': | ||
return new _Error.StripeInvalidGrantError(rawStripeError); | ||
/** | ||
* Helper factory which takes raw stripe errors and outputs wrapping instances | ||
* | ||
* @param {ErrorParams} rawStripeError | ||
*/ | ||
static generate(rawStripeError) { | ||
switch (rawStripeError.type) { | ||
case 'card_error': | ||
return new StripeCardError(rawStripeError); | ||
case 'invalid_request_error': | ||
return new StripeInvalidRequestError(rawStripeError); | ||
case 'api_error': | ||
return new StripeAPIError(rawStripeError); | ||
case 'idempotency_error': | ||
return new StripeIdempotencyError(rawStripeError); | ||
case 'invalid_grant': | ||
return new StripeInvalidGrantError(rawStripeError); | ||
default: | ||
return new GenericError('Generic', 'Unknown Error'); | ||
} | ||
} | ||
return new _Error('Generic', 'Unknown Error'); | ||
}; | ||
} | ||
|
||
// Specific Stripe Error types: | ||
_Error.StripeCardError = StripeError.extend({type: 'StripeCardError'}); | ||
_Error.StripeInvalidRequestError = StripeError.extend({ | ||
type: 'StripeInvalidRequestError', | ||
}); | ||
_Error.StripeAPIError = StripeError.extend({type: 'StripeAPIError'}); | ||
_Error.StripeAuthenticationError = StripeError.extend({ | ||
type: 'StripeAuthenticationError', | ||
}); | ||
_Error.StripePermissionError = StripeError.extend({ | ||
type: 'StripePermissionError', | ||
}); | ||
_Error.StripeRateLimitError = StripeError.extend({ | ||
type: 'StripeRateLimitError', | ||
}); | ||
_Error.StripeConnectionError = StripeError.extend({ | ||
type: 'StripeConnectionError', | ||
}); | ||
_Error.StripeSignatureVerificationError = StripeError.extend({ | ||
type: 'StripeSignatureVerificationError', | ||
}); | ||
_Error.StripeIdempotencyError = StripeError.extend({ | ||
type: 'StripeIdempotencyError', | ||
}); | ||
_Error.StripeInvalidGrantError = StripeError.extend({ | ||
type: 'StripeInvalidGrantError', | ||
}); | ||
|
||
/** | ||
* CardError is raised when a user enters a card that can't be charged for | ||
* some reason. | ||
*/ | ||
class StripeCardError extends StripeError {} | ||
|
||
/** | ||
* InvalidRequestError is raised when a request is initiated with invalid | ||
* parameters. | ||
*/ | ||
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 {} | ||
|
||
/** | ||
* AuthenticationError is raised when invalid credentials are used to connect | ||
* to Stripe's servers. | ||
*/ | ||
class StripeAuthenticationError extends StripeError {} | ||
|
||
/** | ||
* PermissionError is raised in cases where access was attempted on a resource | ||
* that wasn't allowed. | ||
*/ | ||
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 {} | ||
|
||
/** | ||
* 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 {} | ||
|
||
/** | ||
* SignatureVerificationError is raised when the signature verification for a | ||
* webhook fails | ||
*/ | ||
class StripeSignatureVerificationError extends StripeError {} | ||
|
||
/** | ||
* IdempotencyError is raised in cases where an idempotency key was used | ||
* improperly. | ||
*/ | ||
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 {} | ||
|
||
/** | ||
* DEPRECATED: Default import from this module is deprecated and | ||
* will be removed in the next major version | ||
*/ | ||
module.exports = GenericError; | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,12 @@ | |
}, | ||
"main": "lib/stripe.js", | ||
"devDependencies": { | ||
"babel-eslint": "^10.0.1", | ||
"chai": "~4.2.0", | ||
"chai-as-promised": "~7.1.1", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^4.1.0", | ||
"eslint-plugin-chai-friendly": "^0.4.0", | ||
"eslint-plugin-flowtype": "^3.8.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, not sure how that got in there... |
||
"eslint-plugin-prettier": "^3.0.1", | ||
"mocha": "~6.1.4", | ||
"nock": "^10.0.6", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the function most likely to have been used by folks outside of Stripe, if for some reason they decided to define their own errors as an
extend
of some StripeError. Is there a way to preserve it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rattrayalex-stripe did as much as possible (the only case that will be breaking now is default export that is not a function itself). We can do even that, but it will look really weird and for very edge use case.