diff --git a/dist/index.d.ts b/dist/index.d.ts index 84e4410..79117f7 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -12,9 +12,9 @@ export interface ErrorInfo { message: string; name: string; time_thrown: string; - data: string; - path: string; - locations: string; + data?: {}; + path?: string; + locations?: any; } export declare class ApolloError extends ExtendableError { name: string; @@ -24,9 +24,10 @@ export declare class ApolloError extends ExtendableError { path: any; locations: any; _showLocations: boolean; + _showPath: boolean; constructor(name: string, config: ErrorConfig); serialize(): ErrorInfo; } export declare const isInstance: (e: any) => boolean; -export declare const createError: (name: string, config: ErrorConfig) => ApolloError; +export declare const createError: (name: string, config: ErrorConfig) => any; export declare const formatError: (error: any, returnNull?: boolean) => ErrorInfo; diff --git a/dist/index.js b/dist/index.js index ddcd3e6..1598708 100644 --- a/dist/index.js +++ b/dist/index.js @@ -27,6 +27,7 @@ var ApolloError = /** @class */ (function (_super) { function ApolloError(name, config) { var _this = _super.call(this, (arguments[2] && arguments[2].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) || {}; @@ -37,10 +38,11 @@ var ApolloError = /** @class */ (function (_super) { _this.time_thrown = t; _this.data = d; _this._showLocations = !!opts.showLocations; + _this._showPath = !!opts.showPath; return _this; } ApolloError.prototype.serialize = function () { - var _a = this, name = _a.name, message = _a.message, time_thrown = _a.time_thrown, data = _a.data, _showLocations = _a._showLocations, path = _a.path, locations = _a.locations; + var _a = this, name = _a.name, message = _a.message, time_thrown = _a.time_thrown, data = _a.data, _showLocations = _a._showLocations, _showPath = _a._showPath, path = _a.path, locations = _a.locations; var error = { message: message, name: name, @@ -51,6 +53,8 @@ var ApolloError = /** @class */ (function (_super) { }; if (_showLocations) { error.locations = locations; + } + if (_showPath) { error.path = path; } return error; @@ -62,7 +66,7 @@ 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'); - return new ApolloError(name, config); + return ApolloError.bind(null, name, config); }; exports.formatError = function (error, returnNull) { if (returnNull === void 0) { returnNull = false; } @@ -72,10 +76,12 @@ exports.formatError = function (error, returnNull) { var name = originalError.name; if (!name || !exports.isInstance(originalError)) return returnNull ? null : error; - var time_thrown = originalError.time_thrown, message = originalError.message, data = originalError.data, _showLocations = originalError._showLocations; + var time_thrown = originalError.time_thrown, message = originalError.message, data = originalError.data, _showLocations = originalError._showLocations, _showPath = originalError._showPath; + var locations = error.locations, path = error.path; if (_showLocations) { - var locations = error.locations, path = error.path; originalError.locations = locations; + } + if (_showPath) { originalError.path = path; } return originalError.serialize(); diff --git a/dist/index.js.map b/dist/index.js.map index a87c30f..d1908b6 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;IAS/C,qBAAa,IAAW,EAAE,MAAmB;QAA7C,YACC,kBAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,SAanD;QAhBD,oBAAc,GAAU,KAAK,CAAC;QAK7B,IAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,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AAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC,CAAC;AAEW,QAAA,WAAW,GAAG,UAAC,KAAK,EAAE,UAAkB;IAAlB,2BAAA,EAAA,kBAAkB;IACnD,IAAM,aAAa,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,aAAa,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IAElE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC;QAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IAE7C,IAAA,yBAAI,CAAmB;IAE/B,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,kBAAU,CAAC,aAAa,CAAC,CAAC;QAAC,MAAM,CAAC,UAAU,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 6a24e20..ba7b3c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,7 @@ export class ApolloError extends ExtendableError { path: any; locations: any; _showLocations: boolean = false; + _showPath: boolean = false; constructor(name: string, config: ErrorConfig) { super((arguments[2] && arguments[2].message) || ''); @@ -46,10 +47,11 @@ export class ApolloError extends ExtendableError { this.time_thrown = t; this.data = d; this._showLocations = !!opts.showLocations; + this._showPath = !!opts.showPath; } serialize(): ErrorInfo { - const { name, message, time_thrown, data, _showLocations, path, locations } = this; + const { name, message, time_thrown, data, _showLocations, _showPath, path, locations } = this; let error: ErrorInfo = { message, @@ -62,6 +64,9 @@ export class ApolloError extends ExtendableError { if (_showLocations) { error.locations = locations; + } + + if (_showPath) { error.path = path; } @@ -74,7 +79,7 @@ export const isInstance = e => e instanceof ApolloError; export const createError = (name: string, config: ErrorConfig) => { 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'); - return new ApolloError(name, config); + return ApolloError.bind(null, name, config); }; export const formatError = (error, returnNull = false): ErrorInfo => { @@ -86,11 +91,14 @@ export const formatError = (error, returnNull = false): ErrorInfo => { if (!name || !isInstance(originalError)) return returnNull ? null : error; - const { time_thrown, message, data, _showLocations } = originalError; + const { time_thrown, message, data, _showLocations, _showPath } = originalError; + const { locations, path } = error; if (_showLocations) { - const { locations, path } = error; originalError.locations = locations; + } + + if (_showPath) { originalError.path = path; }