-
Notifications
You must be signed in to change notification settings - Fork 879
Expand file tree
/
Copy pathError.js
More file actions
75 lines (65 loc) · 2.46 KB
/
Error.js
File metadata and controls
75 lines (65 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
var utils = require('./utils');
module.exports = _Error;
/**
* Generic Error klass to wrap any errors returned by stripe-node
*/
function _Error(raw) {
this.populate.apply(this, arguments);
this.stack = (new Error(this.message)).stack;
}
// Extend Native Error
_Error.prototype = Object.create(Error.prototype);
_Error.prototype.type = 'GenericError';
_Error.prototype.populate = function(type, message) {
this.type = type;
this.message = message;
};
_Error.extend = utils.protoExtend;
/**
* Create subclass of internal Error klass
* (Specifically for errors returned from Stripe's REST API)
*/
var StripeError = _Error.StripeError = _Error.extend({
type: 'StripeError',
populate: function(raw) {
// Move from prototype def (so it appears in stringified obj)
this.type = this.type;
this.stack = (new Error(raw.message)).stack;
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;
},
});
/**
* Helper factory which takes raw stripe errors and outputs wrapping instances
*/
StripeError.generate = function(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);
}
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'});