Skip to content

Commit

Permalink
shim private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tinovyatkin committed Jul 30, 2019
1 parent 126447c commit f3528b4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
52 changes: 38 additions & 14 deletions lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,63 @@
* Generic Error class to wrap any errors returned by stripe-node
*/
class GenericError extends Error {
constructor(message) {
constructor(type, message) {

This comment has been minimized.

Copy link
@rattrayalex-stripe

rattrayalex-stripe Jul 30, 2019

Contributor

hmmm, this doesn't look quite right to me... for one, it's definitely a breaking change, and for another, I don't think it should be necessary?

I think instead of changing this class at all, we should set static properties on customError where it is created...

super(message);
// Saving class name in the property of our custom error as a shortcut.
this.type = this.constructor.name;
this.name = this.constructor.name;
this.type = type || this.constructor.name;
this.name = type || this.constructor.name;

// Capturing stack trace, excluding constructor call from it.
Error.captureStackTrace(this, this.constructor);
}

populate(raw) {
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.headers = raw.headers;
this.requestId = raw.requestId;
this.statusCode = raw.statusCode;
}

/**
* @param {{ type: string, message?: string, [k:string]: any }} options
*/
static extend(options) {
class customError extends GenericError {
constructor(raw = {}) {
super(options.type, raw.message);
this.populate(raw);
}
}
for (const property in options) {
customError.prototype[property] = options[property];
}
return customError;
}
}

/**
* StripeError is the base error from which all other more specific Stripe
* errors derive.
* (Specifically for errors returned from Stripe's REST API)
*
* @typedef {{ message: string, type?: string, code?: number, param?: string, detail: string, headers?: Record<string, string>, requestId?: string, statusCode?: number }} ErrorParams
* @typedef {{ message?: string, type?: string, code?: number, param?: string, detail?: string, headers?: Record<string, string>, requestId?: string, statusCode?: number }} ErrorParams
*/
class StripeError extends GenericError {
/**
*
* @param {ErrorParams} raw
*/
constructor(raw) {
super(raw.message);
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;
super(undefined, raw.message);
this.populate(raw);
}

/**
Expand Down Expand Up @@ -144,4 +167,5 @@ module.exports = {
StripeSignatureVerificationError,
StripeIdempotencyError,
StripeInvalidGrantError,
extend: GenericError.extend.bind(GenericError),
};
32 changes: 30 additions & 2 deletions test/Error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const expect = require('chai').expect;

describe('Error', () => {
it('Populates with type and message params', () => {
const e = new Error.GenericError('Foo happened');
expect(e).to.have.property('type', 'GenericError');
const e = new Error.GenericError('FooError', 'Foo happened');
expect(e).to.have.property('type', 'FooError');
expect(e).to.have.property('message', 'Foo happened');
expect(e).to.have.property('stack');
});
Expand Down Expand Up @@ -53,5 +53,33 @@ describe('Error', () => {
});
expect(e).to.have.property('statusCode', 400);
});

it('can be extended via .extend method', () => {
const Custom = Error.GenericError.extend({type: 'MyCustomErrorType'});
const err = new Custom({message: 'byaka'});
expect(err).to.be.instanceOf(Error.GenericError);
expect(err).to.have.property('type', 'MyCustomErrorType');
expect(err).to.have.property('name', 'MyCustomErrorType');
expect(err).to.have.property('message', 'byaka');
});

it('can create custom error via `extend` export', () => {
const Custom = Error.extend({
type: 'MyCardError',
populate(raw) {
this.detail = 'hello';
this.customField = 'hi';
},
});
const err = new Custom({
message: 'ee',
});
expect(err).to.be.instanceOf(Error.GenericError);
expect(err).to.have.property('type', 'MyCardError');
expect(err).to.have.property('name', 'MyCardError');
expect(err).to.have.property('message', 'ee');
expect(err).to.have.property('detail', 'hello');
expect(err).to.have.property('customField', 'hi');
});
});
});

0 comments on commit f3528b4

Please sign in to comment.