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/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..84e4410 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,32 @@ +import ExtendableError from 'extendable-error'; +export interface ErrorConfig { + 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; +} +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..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,C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+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.6.3", + "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" } diff --git a/src/index.ts b/src/index.ts index a2aec2f..6a24e20 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,29 +4,41 @@ 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 { +export interface ErrorConfig { message: string; + time_thrown?: string; + data?: any; + options?: { + showPath?: boolean; + showLocations?: boolean; + }; +} + +export interface ErrorInfo { + message: string; + name: string; time_thrown: string; - data: any, - options: any, + data?: {}; + path?: string; + locations?: any; } -class ApolloError extends ExtendableError { +export class ApolloError extends ExtendableError { name: string; message: string; time_thrown: string; data: any; path: any; locations: any; - _showLocations: boolean=false; + _showLocations: boolean = false; - constructor (name:string, config: ErrorConfig) { + 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 d = { ...this.data, ...configData } const opts = ((arguments[2] && arguments[2].options) || {}) this.name = name; @@ -35,10 +47,11 @@ class ApolloError extends ExtendableError { this.data = d; this._showLocations = !!opts.showLocations; } - serialize () { + + serialize(): ErrorInfo { const { name, message, time_thrown, data, _showLocations, path, locations } = this; - let error = { + let error: ErrorInfo = { message, name, time_thrown, @@ -46,24 +59,25 @@ class ApolloError extends ExtendableError { 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) => { +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; + return new ApolloError(name, config); }; -export const formatError = (error, returnNull = false) => { +export const formatError = (error, returnNull = false): ErrorInfo => { const originalError = error ? error.originalError || error : null; if (!originalError) return returnNull ? null : error; diff --git a/tsconfig.json b/tsconfig.json index f0b096c..a84597b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,8 +6,8 @@ "sourceMap": true, "removeComments": false, "noImplicitAny": false, - "allowJs": true, - "outDir": "./dist" + "outDir": "./dist", + "declaration": true }, "include": [ "./src/**/*"