Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# intellij idea
.idea


# Logs
logs
*.log
Expand Down
32 changes: 32 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 43 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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 <[email protected]>",
"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"
}
40 changes: 27 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,35 +47,37 @@ 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,
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) => {
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;
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"sourceMap": true,
"removeComments": false,
"noImplicitAny": false,
"allowJs": true,
"outDir": "./dist"
"outDir": "./dist",
"declaration": true
},
"include": [
"./src/**/*"
Expand Down