diff --git a/dist/index.d.ts b/dist/index.d.ts index 6634488..26cb1cc 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,6 +1,6 @@ import ExtendableError from 'extendable-error'; export interface ErrorConfig { - message: string; + message?: string; time_thrown?: string; data?: object; options?: { @@ -25,7 +25,7 @@ export declare class ApolloError extends ExtendableError { locations: any; _showLocations: boolean; _showPath: boolean; - constructor(name: string, config: ErrorConfig); + constructor(name: string, baseConfig?: ErrorConfig, config?: ErrorConfig); serialize(): ErrorInfo; } export declare const isInstance: (e: any) => boolean; diff --git a/dist/index.js b/dist/index.js index 1598708..2aed4c1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -24,21 +24,19 @@ 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) { - var _this = _super.call(this, (arguments[2] && arguments[2].message) || '') || this; + function ApolloError(name, baseConfig, config) { + if (baseConfig === void 0) { baseConfig = {}; } + if (config === void 0) { config = {}; } + var _this = _super.call(this, config.message || baseConfig.message || '') || this; _this._showLocations = false; _this._showPath = false; - var t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString(); - var m = (arguments[2] && arguments[2].message) || ''; - var configData = (arguments[2] && arguments[2].data) || {}; - var d = __assign({}, _this.data, configData); - var opts = ((arguments[2] && arguments[2].options) || {}); + var options = __assign({}, config.options, baseConfig.options); _this.name = name; - _this.message = m; - _this.time_thrown = t; - _this.data = d; - _this._showLocations = !!opts.showLocations; - _this._showPath = !!opts.showPath; + _this.message = config.message || baseConfig.message || ''; + _this.time_thrown = config.time_thrown || baseConfig.time_thrown || new Date().toISOString(); + _this.data = __assign({}, baseConfig.data, config.data); + _this._showLocations = !!options.showLocations; + _this._showPath = !!options.showPath; return _this; } ApolloError.prototype.serialize = function () { diff --git a/dist/index.js.map b/dist/index.js.map index 923a414..9b44123 100644 --- a/dist/index.js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AU,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IAElE,IAAA,uCAAW,EAAE,+BAAO,EAAE,yBAAI,EAAE,6CAAc,EAAE,mCAAS,CAAmB;IACxE,IAAA,2BAAS,EAAE,iBAAI,CAAW;IAElC,EAAE,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC;QACnB,aAAa,CAAC,SAAS,GAAG,SAAS,CAAC;IACtC,CAAC;IAED,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;QACd,aAAa,CAAC,IAAI,GAAG,IAAI,CAAC;IAC5B,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC;AACnC,CAAC,CAAC"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index cb9d9d5..d50ab23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ const isString = d => Object.prototype.toString.call(d) === '[object String]'; const isObject = d => Object.prototype.toString.call(d) === '[object Object]'; export interface ErrorConfig { - message: string; + message?: string; time_thrown?: string; data?: object; options?: { @@ -33,22 +33,17 @@ export class ApolloError extends ExtendableError { _showLocations: boolean = false; _showPath: boolean = false; - constructor(name: string, config: ErrorConfig) { - super((arguments[2] && arguments[2].message) || ''); - - const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString(); - const m = (arguments[2] && arguments[2].message) || ''; - const configData = (arguments[2] && arguments[2].data) || {}; - const d = { ...this.data, ...configData }; - const opts = ((arguments[2] && arguments[2].options) || {}); - + constructor(name: string, baseConfig: ErrorConfig = {}, config: ErrorConfig = {}) { + super(config.message || baseConfig.message || ''); + + const options = {...config.options, ...baseConfig.options}; this.name = name; - this.message = m; - this.time_thrown = t; - this.data = d; - this._showLocations = !!opts.showLocations; - this._showPath = !!opts.showPath; + this.message = config.message || baseConfig.message || ''; + this.time_thrown = config.time_thrown || baseConfig.time_thrown || new Date().toISOString(); + this.data = {...baseConfig.data, ...config.data}; + this._showLocations = !!options.showLocations; + this._showPath = !!options.showPath; } serialize(): ErrorInfo { diff --git a/test/spec.js b/test/spec.js index 9036ac4..5ac097c 100644 --- a/test/spec.js +++ b/test/spec.js @@ -40,6 +40,26 @@ describe('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', () => {