Skip to content

Commit

Permalink
Merge pull request #19 from lakhansamani/typescript
Browse files Browse the repository at this point in the history
#16 Written error class in typescript
  • Loading branch information
thebigredgeek authored Sep 25, 2017
2 parents e883f2e + 5665a6a commit 7fce29b
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
logs
*.log
npm-debug.log*
package-lock.json

# Runtime data
pids
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ endif
.FORCE:

all: clean
babel src -d dist --source-maps
tsc

clean: .FORCE
rimraf npm-debug.log dist
Expand Down
205 changes: 78 additions & 127 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.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
"dependencies": {
"es6-error": "^4.0.0"
"assert": "^1.4.1",
"extendable-error": "^0.1.5"
},
"devDependencies": {
"babel-cli": "^6.18.0",
Expand All @@ -37,6 +38,7 @@
"eslint": "^3.8.1",
"eslint-plugin-babel": "^3.3.0",
"mocha": "^3.1.2",
"rimraf": "^2.5.4"
"rimraf": "^2.5.4",
"typescript": "^2.5.2"
}
}
45 changes: 28 additions & 17 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import assert from 'assert';
import ExtendableError from 'es6-error';
import * as assert from 'assert';
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,
}

class ApolloError extends ExtendableError {
constructor (name, {
message,
time_thrown = (new Date()).toISOString(),
data = {},
options = {},
}) {
const t = (arguments[2] && arguments[2].time_thrown) || time_thrown;
const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));
const m = (arguments[2] && arguments[2].message) || message;
const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));

super(m);
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;
Expand All @@ -32,20 +43,20 @@ class ApolloError extends ExtendableError {
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, 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');
const e = ApolloError.bind(null, name, config);
Expand Down
Loading

0 comments on commit 7fce29b

Please sign in to comment.