diff --git a/dist/index.d.ts b/dist/index.d.ts index 09cd18e..1fb6aac 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -25,7 +25,7 @@ export declare class ApolloError extends ExtendableError { locations: any; _showLocations: boolean; _showPath: boolean; - constructor(name: string, config: ErrorConfig, ctorData: any); + constructor(name: string, config: ErrorConfig, ctorConfig: ErrorConfig); serialize(): ErrorInfo; } export declare const isInstance: (e: any) => boolean; diff --git a/dist/index.js b/dist/index.js index f0798fc..663e89d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -24,15 +24,21 @@ var isString = function (d) { return Object.prototype.toString.call(d) === '[obj var isObject = function (d) { return Object.prototype.toString.call(d) === '[object Object]'; }; var ApolloError = /** @class */ (function (_super) { __extends(ApolloError, _super); - function ApolloError(name, config, ctorData) { - var _this = _super.call(this, (ctorData && ctorData.message) || '') || this; + // NOTE: The object passed to the Constructor is actually `ctorData`. + // We are binding the constructor to the name and config object + // for the first two parameters inside of `createError` + function ApolloError(name, config, ctorConfig) { + var _this = _super.call(this, (ctorConfig && ctorConfig.message) || (config && config.message) || '') || this; _this._showLocations = false; _this._showPath = false; - var t = (ctorData && ctorData.time_thrown) || (new Date()).toISOString(); - var m = (ctorData && ctorData.message) || ''; - var configData = (ctorData && ctorData.data) || {}; - var d = __assign({}, _this.data, configData); - var opts = ((ctorData && ctorData.options) || {}); + var t = (ctorConfig && ctorConfig.time_thrown) || (config && config.time_thrown) || (new Date()).toISOString(); + var m = (ctorConfig && ctorConfig.message) || (config && config.message) || ''; + var ctorData = (ctorConfig && ctorConfig.data) || {}; + var configData = (config && config.data) || {}; + var d = __assign({}, _this.data, configData, ctorData); + var ctorOptions = (ctorConfig && ctorConfig.options) || {}; + var configOptions = (config && config.options) || {}; + var opts = __assign({}, configOptions, ctorOptions); _this.name = name; _this.message = m; _this.time_thrown = t; @@ -66,6 +72,9 @@ exports.isInstance = function (e) { return e instanceof ApolloError; }; exports.createError = function (name, config) { assert(isObject(config), 'createError requires a config object as the second parameter'); assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter'); + // NOTE: The first two parameters give to the constructor will always be name and config + // Parameters passed to the constructor when `new` is invoked will be passed as + // subsequent parameters. return ApolloError.bind(null, name, config); }; exports.formatError = function (error, returnNull) { diff --git a/dist/index.js.map b/dist/index.js.map index a51ade4..98e6395 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,+BAAiC;AACjC,qDAA+C;AAE/C,IAAM,QAAQ,GAAG,UAAA,CAAC,IAAI,OAAA,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,iBAAiB,EAAvD,CAAuD,CAAC;AAC9E,IAAM,QAAQ,GAAG,UAAA,CAAC,IAAI,OAAA,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,iBAAiB,EAAvD,CAAuD,CAAC;AAqB9E;IAAiC,+BAAe;IAU9C,qBAAY,IAAY,EAAE,MAAmB,EAAE,QAAa;QAA5D,YACE,kBAAM,CAAC,QAAQ,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,SAe5C;QAnBD,oBAAc,GAAY,KAAK,CAAC;QAChC,eAAS,GAAY,KAAK,CAAC;QAKzB,IAAM,CAAC,GAAG,CAAC,QAAQ,IAAI,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;QAC3E,IAAM,CAAC,GAAG,CAAC,QAAQ,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;QAC/C,IAAM,UAAU,GAAG,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QACrD,IAAM,CAAC,gBAAQ,KAAI,CAAC,IAAI,EAAK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('createError', () => { foo: 'bar' }); }); + it('uses original message and merges original data', () => { + const FooError = createError('FooError', { + message: 'A foo error has occurred', + data: { + hello: 'world' + }, + options: { + showLocations: false, + showPath: true + } + }); + const e = new FooError({data: {foo: 'bar'}}); + + const { message, name, time_thrown, data } = e.serialize(); + expect(message).to.equal('A foo error has occurred'); + expect(name).to.equal('FooError'); + expect(time_thrown).to.equal(e.time_thrown); + expect(data).to.eql({ hello: 'world', foo: 'bar' }); + }); }); context('when missing a config as the second parameter', () => { it('throws an assertion error with a useful message', () => {