From 3450ef252f456185ab443ce8042ae096cc7a3b46 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:15:47 +0300 Subject: [PATCH 01/10] added ts types --- dist/index.d.ts | 29 ++++++++++ dist/index.js | 4 +- dist/index.js.map | 2 +- src/index.ts | 135 +++++++++++++++++++++++++--------------------- tsconfig.json | 6 ++- 5 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 dist/index.d.ts diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..dd1ddbd --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,29 @@ +import ExtendableError from 'extendable-error'; +export interface ErrorConfig { + message: string; + time_thrown: string; + data: any; + options: any; +} +export interface ErrorInfo { + message: string; + name: string; + time_thrown: string; + data: string; + path: string; + locations: string; +} +export declare class ApolloError extends ExtendableError { + name: string; + message: string; + time_thrown: string; + data: any; + path: any; + locations: any; + _showLocations: 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 formatError: (error: any, returnNull?: boolean) => ErrorInfo; diff --git a/dist/index.js b/dist/index.js index 12bcca6..ddcd3e6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -57,12 +57,12 @@ var ApolloError = /** @class */ (function (_super) { }; return ApolloError; }(extendable_error_1.default)); +exports.ApolloError = ApolloError; 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'); - var e = ApolloError.bind(null, name, config); - return e; + return new ApolloError(name, config); }; exports.formatError = function (error, returnNull) { if (returnNull === void 0) { returnNull = false; } diff --git a/dist/index.js.map b/dist/index.js.map index 529bd5f..ef2ed71 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../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;IAED,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC;AAClC,CAAC,CAAC"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a2aec2f..b340f6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,81 +4,92 @@ import ExtendableError from 'extendable-error'; const isString = d => Object.prototype.toString.call(d) === '[object String]'; const isObject = d => Object.prototype.toString.call(d) === '[object Object]'; -interface ErrorConfig { - message: string; - time_thrown: string; - data: any, - options: any, +export interface ErrorConfig { + message: string; + time_thrown: string; + data: any, + options: any, } -class ApolloError extends ExtendableError { - name: string; - message: string; - time_thrown: string; - data: any; - path: any; - locations: any; - _showLocations: 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) || {}) - - this.name = name; - this.message = m; - this.time_thrown = t; - this.data = d; - this._showLocations = !!opts.showLocations; - } - serialize () { - const { name, message, time_thrown, data, _showLocations, path, locations } = this; - - let error = { - message, - name, - time_thrown, - data, - path, - locations - }; - if (_showLocations) { - error.locations = locations; - error.path = path; - } - return error; - } +export interface ErrorInfo { + message: string; + name: string; + time_thrown: string; + data: string; + path: string; + locations: string; +} + +export class ApolloError extends ExtendableError { + name: string; + message: string; + time_thrown: string; + data: any; + path: any; + locations: any; + _showLocations: 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) || {}) + + this.name = name; + this.message = m; + this.time_thrown = t; + this.data = d; + this._showLocations = !!opts.showLocations; + } + + serialize (): ErrorInfo { + const { name, message, time_thrown, data, _showLocations, path, locations } = this; + + let error: ErrorInfo = { + message, + name, + time_thrown, + data, + path, + locations + }; + + if (_showLocations) { + error.locations = locations; + error.path = path; + } + + return error; + } } 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'); - const e = ApolloError.bind(null, name, config); - return e; + 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); }; -export const formatError = (error, returnNull = false) => { - const originalError = error ? error.originalError || error : null; +export const formatError = (error, returnNull = false): ErrorInfo => { + const originalError = error ? error.originalError || error : null; - if (!originalError) return returnNull ? null : error; + if (!originalError) return returnNull ? null : error; - const { name } = originalError; + const { name } = originalError; - if (!name || !isInstance(originalError)) return returnNull ? null : error; + if (!name || !isInstance(originalError)) return returnNull ? null : error; - const { time_thrown, message, data, _showLocations } = originalError; + const { time_thrown, message, data, _showLocations } = originalError; - if (_showLocations) { - const { locations, path } = error; - originalError.locations = locations; - originalError.path = path; - } + if (_showLocations) { + const { locations, path } = error; + originalError.locations = locations; + originalError.path = path; + } - return originalError.serialize(); + return originalError.serialize(); }; diff --git a/tsconfig.json b/tsconfig.json index f0b096c..bd7c8da 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,8 +6,10 @@ "sourceMap": true, "removeComments": false, "noImplicitAny": false, - "allowJs": true, - "outDir": "./dist" + // "allowJs": true, + "outDir": "./dist", + // "emitDecoratorMetadata": true, + "declaration": true }, "include": [ "./src/**/*" From 13c29c97330ac90f066e0a2f03826a825ba05d96 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:19:45 +0300 Subject: [PATCH 02/10] package.json typings update --- package.json | 85 ++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index eb3a19f..93c7821 100644 --- a/package.json +++ b/package.json @@ -1,44 +1,45 @@ { - "name": "apollo-errors", - "version": "1.5.1", - "description": "Machine-readable custom errors for Apollostack's GraphQL server", - "main": "dist/index.js", - "scripts": { - "test": "make test" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/thebigredgeek/apollo-errors.git" - }, - "keywords": [ - "apollostack", - "graphql", - "apollo-server", - "apollo-client", - "error", - "api" - ], - "author": "Andrew E. Rhyne ", - "license": "MIT", - "bugs": { - "url": "https://github.com/thebigredgeek/apollo-errors/issues" - }, - "homepage": "https://github.com/thebigredgeek/apollo-errors#readme", - "dependencies": { - "assert": "^1.4.1", - "extendable-error": "^0.1.5" - }, - "devDependencies": { - "babel-cli": "^6.18.0", - "babel-core": "^6.17.0", - "babel-eslint": "^7.0.0", - "babel-preset-es2015": "^6.16.0", - "babel-register": "^6.18.0", - "chai": "^3.5.0", - "eslint": "^3.8.1", - "eslint-plugin-babel": "^3.3.0", - "mocha": "^3.1.2", - "rimraf": "^2.5.4", - "typescript": "^2.5.2" - } + "name": "apollo-errors", + "version": "1.5.1", + "description": "Machine-readable custom errors for Apollostack's GraphQL server", + "main": "dist/index.js", + "scripts": { + "test": "make test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/thebigredgeek/apollo-errors.git" + }, + "keywords": [ + "apollostack", + "graphql", + "apollo-server", + "apollo-client", + "error", + "api" + ], + "author": "Andrew E. Rhyne ", + "license": "MIT", + "bugs": { + "url": "https://github.com/thebigredgeek/apollo-errors/issues" + }, + "homepage": "https://github.com/thebigredgeek/apollo-errors#readme", + "dependencies": { + "assert": "^1.4.1", + "extendable-error": "^0.1.5" + }, + "devDependencies": { + "babel-cli": "^6.18.0", + "babel-core": "^6.17.0", + "babel-eslint": "^7.0.0", + "babel-preset-es2015": "^6.16.0", + "babel-register": "^6.18.0", + "chai": "^3.5.0", + "eslint": "^3.8.1", + "eslint-plugin-babel": "^3.3.0", + "mocha": "^3.1.2", + "rimraf": "^2.5.4", + "typescript": "^2.5.2" + }, + "typings": "dist/index.d.ts" } From 6bb3e2923f9d22d8ba8845c6015f6276de8b46ed Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:26:49 +0300 Subject: [PATCH 03/10] updated ErrorConfig interface --- dist/index.d.ts | 6 +++--- package.json | 2 +- src/index.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index dd1ddbd..a072147 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,9 +1,9 @@ import ExtendableError from 'extendable-error'; export interface ErrorConfig { message: string; - time_thrown: string; - data: any; - options: any; + time_thrown?: string; + data?: any; + options?: any; } export interface ErrorInfo { message: string; diff --git a/package.json b/package.json index 93c7821..6ba936f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-errors", - "version": "1.5.1", + "version": "1.6.1", "description": "Machine-readable custom errors for Apollostack's GraphQL server", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index b340f6f..a1e6002 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,9 +6,9 @@ const isObject = d => Object.prototype.toString.call(d) === '[object Object]'; export interface ErrorConfig { message: string; - time_thrown: string; - data: any, - options: any, + time_thrown?: string; + data?: any, + options?: any, } export interface ErrorInfo { From 18b8dd990d302b2f8cef0ca9bf38e3dee3acc033 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:29:36 +0300 Subject: [PATCH 04/10] updated ErrorConfig interface --- src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a1e6002..e373c77 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,8 +7,11 @@ const isObject = d => Object.prototype.toString.call(d) === '[object Object]'; export interface ErrorConfig { message: string; time_thrown?: string; - data?: any, - options?: any, + data?: any; + options?: { + showPath?: boolean; + showLocations?: boolean; + }, } export interface ErrorInfo { From 701972f9f879e32936e64d25a87289a6beba14c9 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:29:48 +0300 Subject: [PATCH 05/10] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6ba936f..a147fdf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-errors", - "version": "1.6.1", + "version": "1.6.2", "description": "Machine-readable custom errors for Apollostack's GraphQL server", "main": "dist/index.js", "scripts": { From cb7bcf984220dcead4bdbbf36ad98b66969943d8 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:30:29 +0300 Subject: [PATCH 06/10] build --- dist/index.d.ts | 5 ++++- dist/index.js.map | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index a072147..84e4410 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -3,7 +3,10 @@ export interface ErrorConfig { message: string; time_thrown?: string; data?: any; - options?: any; + options?: { + showPath?: boolean; + showLocations?: boolean; + }; } export interface ErrorInfo { message: string; diff --git a/dist/index.js.map b/dist/index.js.map index ef2ed71..a87c30f 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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C,CAAC;IACzF,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,+FAA+F,CAAC,CAAC;IAClI,MAAM,CAAC,IAAI,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AACtC,CAAC,CAAC;AAEW,QAAA,WAAW,GAAG,UAAC,KAAK,EAAE,UAAkB;IAAlB,2BAAA,EAAA,kBAAkB;IACpD,IAAM,aAAa,GAAG,KAAK,GAAG,KAAK,CAAC,aAAa,IAAI,KAAK,GAAG,IAAI,CAAC;IAElE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC;QAAC,MAAM,CAAC,UAAU,GAAG,IAAI,GAAG,KAAK,CAAC;IAE7C,IAAA,yBAAI,CAAmB;IAE/B,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,kBAAU,CAAC,aAAa,CAAC,CAAC;QAAC,MAAM,CAAC,UAAU,GAAG,IAAI,GAAG,KAAK,CAAC;IAElE,IAAA,uCAAW,EAAE,+BAAO,EAAE,yBAAI,EAAE,6CAAc,CAAmB;IAErE,EAAE,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC;QACZ,IAAA,2BAAS,EAAE,iBAAI,CAAW;QAClC,aAAa,CAAC,SAAS,GAAG,SAAS,CAAC;QACpC,aAAa,CAAC,IAAI,GAAG,IAAI,CAAC;IAC3B,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC;AAClC,CAAC,CAAC"} \ No newline at end of file From 431c15f850b252d79ee0b72ffd704d4a9a4342ca Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:36:47 +0300 Subject: [PATCH 07/10] cleaned up tsconfig --- package.json | 2 +- tsconfig.json | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index a147fdf..01a1fbe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-errors", - "version": "1.6.2", + "version": "1.6.3", "description": "Machine-readable custom errors for Apollostack's GraphQL server", "main": "dist/index.js", "scripts": { diff --git a/tsconfig.json b/tsconfig.json index bd7c8da..a84597b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,9 +6,7 @@ "sourceMap": true, "removeComments": false, "noImplicitAny": false, - // "allowJs": true, "outDir": "./dist", - // "emitDecoratorMetadata": true, "declaration": true }, "include": [ From 1ea7a07f6032bed1e2550d85eb71081db9897df7 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Tue, 24 Oct 2017 21:42:13 +0300 Subject: [PATCH 08/10] repaired indentation --- .gitignore | 4 ++ src/index.ts | 142 +++++++++++++++++++++++++-------------------------- 2 files changed, 75 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index dd71061..9c6fc03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# intellij idea +.idea + + # Logs logs *.log diff --git a/src/index.ts b/src/index.ts index e373c77..40e3d5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,94 +5,94 @@ 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; - time_thrown?: string; - data?: any; - options?: { - showPath?: boolean; - showLocations?: boolean; - }, + message: string; + time_thrown?: string; + data?: any; + options?: { + showPath?: boolean; + showLocations?: boolean; + }; } export interface ErrorInfo { - message: string; - name: string; - time_thrown: string; - data: string; - path: string; - locations: string; + message: string; + name: string; + time_thrown: string; + data: string; + path: string; + locations: string; } export class ApolloError extends ExtendableError { - name: string; - message: string; - time_thrown: string; - data: any; - path: any; - locations: any; - _showLocations: 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) || {}) - - this.name = name; - this.message = m; - this.time_thrown = t; - this.data = d; - this._showLocations = !!opts.showLocations; - } - - serialize (): ErrorInfo { - const { name, message, time_thrown, data, _showLocations, path, locations } = this; - - let error: ErrorInfo = { - message, - name, - time_thrown, - data, - path, - locations - }; - - if (_showLocations) { - error.locations = locations; - error.path = path; - } - - return error; - } + name: string; + message: string; + time_thrown: string; + data: any; + path: any; + locations: any; + _showLocations: 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) || {}) + + this.name = name; + this.message = m; + this.time_thrown = t; + this.data = d; + this._showLocations = !!opts.showLocations; + } + + serialize(): ErrorInfo { + const { name, message, time_thrown, data, _showLocations, path, locations } = this; + + let error: ErrorInfo = { + message, + name, + time_thrown, + data, + path, + locations + }; + + if (_showLocations) { + error.locations = locations; + error.path = path; + } + + return error; + } } 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); +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); }; export const formatError = (error, returnNull = false): ErrorInfo => { - const originalError = error ? error.originalError || error : null; + const originalError = error ? error.originalError || error : null; - if (!originalError) return returnNull ? null : error; + if (!originalError) return returnNull ? null : error; - const { name } = originalError; + const { name } = originalError; - if (!name || !isInstance(originalError)) return returnNull ? null : error; + if (!name || !isInstance(originalError)) return returnNull ? null : error; - const { time_thrown, message, data, _showLocations } = originalError; + const { time_thrown, message, data, _showLocations } = originalError; - if (_showLocations) { - const { locations, path } = error; - originalError.locations = locations; - originalError.path = path; - } + if (_showLocations) { + const { locations, path } = error; + originalError.locations = locations; + originalError.path = path; + } - return originalError.serialize(); + return originalError.serialize(); }; From a354487764f7684ea52c23b88809a93e32efc289 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Thu, 26 Oct 2017 16:27:21 +0300 Subject: [PATCH 09/10] ErrorInfo optional fields --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 40e3d5d..250b109 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,9 +18,9 @@ export interface ErrorInfo { message: string; name: string; time_thrown: string; - data: string; - path: string; - locations: string; + data?: string; + path?: string; + locations?: string; } export class ApolloError extends ExtendableError { From d84ea3fb5da7909c8d7f0a87350f506f2e91cd38 Mon Sep 17 00:00:00 2001 From: Daniel Khoroshko Date: Thu, 26 Oct 2017 16:29:44 +0300 Subject: [PATCH 10/10] ErrorInfo field types --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 250b109..6a24e20 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,9 +18,9 @@ export interface ErrorInfo { message: string; name: string; time_thrown: string; - data?: string; + data?: {}; path?: string; - locations?: string; + locations?: any; } export class ApolloError extends ExtendableError {