Skip to content

Commit 7fce29b

Browse files
Merge pull request #19 from lakhansamani/typescript
#16 Written error class in typescript
2 parents e883f2e + 5665a6a commit 7fce29b

File tree

8 files changed

+129
-152
lines changed

8 files changed

+129
-152
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
logs
33
*.log
44
npm-debug.log*
5+
package-lock.json
56

67
# Runtime data
78
pids

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ endif
1313
.FORCE:
1414

1515
all: clean
16-
babel src -d dist --source-maps
16+
tsc
1717

1818
clean: .FORCE
1919
rimraf npm-debug.log dist

dist/index.js

Lines changed: 78 additions & 127 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
},
2626
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
2727
"dependencies": {
28-
"es6-error": "^4.0.0"
28+
"assert": "^1.4.1",
29+
"extendable-error": "^0.1.5"
2930
},
3031
"devDependencies": {
3132
"babel-cli": "^6.18.0",
@@ -37,6 +38,7 @@
3738
"eslint": "^3.8.1",
3839
"eslint-plugin-babel": "^3.3.0",
3940
"mocha": "^3.1.2",
40-
"rimraf": "^2.5.4"
41+
"rimraf": "^2.5.4",
42+
"typescript": "^2.5.2"
4143
}
4244
}

src/index.js renamed to src/index.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
import assert from 'assert';
2-
import ExtendableError from 'es6-error';
1+
import * as assert from 'assert';
2+
import ExtendableError from 'extendable-error';
33

44
const isString = d => Object.prototype.toString.call(d) === '[object String]';
55
const isObject = d => Object.prototype.toString.call(d) === '[object Object]';
66

7+
interface ErrorConfig {
8+
message: string;
9+
time_thrown: string;
10+
data: any,
11+
options: any,
12+
}
13+
714
class ApolloError extends ExtendableError {
8-
constructor (name, {
9-
message,
10-
time_thrown = (new Date()).toISOString(),
11-
data = {},
12-
options = {},
13-
}) {
14-
const t = (arguments[2] && arguments[2].time_thrown) || time_thrown;
15-
const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));
16-
const m = (arguments[2] && arguments[2].message) || message;
17-
const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));
18-
19-
super(m);
15+
name: string;
16+
message: string;
17+
time_thrown: string;
18+
data: any;
19+
path: any;
20+
locations: any;
21+
_showLocations: boolean=false;
22+
23+
constructor (name:string, config: ErrorConfig) {
24+
super((arguments[2] && arguments[2].message) || '');
25+
26+
const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString();
27+
const m = (arguments[2] && arguments[2].message) || '';
28+
const configData = (arguments[2] && arguments[2].data) || {};
29+
const d = {...this.data, ...configData}
30+
const opts = ((arguments[2] && arguments[2].options) || {})
2031

2132
this.name = name;
2233
this.message = m;
@@ -32,20 +43,20 @@ class ApolloError extends ExtendableError {
3243
name,
3344
time_thrown,
3445
data,
46+
path,
47+
locations
3548
};
36-
3749
if (_showLocations) {
3850
error.locations = locations;
3951
error.path = path;
4052
}
41-
4253
return error;
4354
}
4455
}
4556

4657
export const isInstance = e => e instanceof ApolloError;
4758

48-
export const createError = (name, config) => {
59+
export const createError = (name:string, config: ErrorConfig) => {
4960
assert(isObject(config), 'createError requires a config object as the second parameter');
5061
assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter');
5162
const e = ApolloError.bind(null, name, config);

0 commit comments

Comments
 (0)