1
- import assert from 'assert' ;
2
- import ExtendableError from 'es6 -error' ;
1
+ import * as assert from 'assert' ;
2
+ import ExtendableError from 'extendable -error' ;
3
3
4
4
const isString = d => Object . prototype . toString . call ( d ) === '[object String]' ;
5
5
const isObject = d => Object . prototype . toString . call ( d ) === '[object Object]' ;
6
6
7
+ interface ErrorConfig {
8
+ message : string ;
9
+ time_thrown : string ;
10
+ data : any ,
11
+ options : any ,
12
+ }
13
+
7
14
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 ) || { } )
20
31
21
32
this . name = name ;
22
33
this . message = m ;
@@ -32,20 +43,20 @@ class ApolloError extends ExtendableError {
32
43
name,
33
44
time_thrown,
34
45
data,
46
+ path,
47
+ locations
35
48
} ;
36
-
37
49
if ( _showLocations ) {
38
50
error . locations = locations ;
39
51
error . path = path ;
40
52
}
41
-
42
53
return error ;
43
54
}
44
55
}
45
56
46
57
export const isInstance = e => e instanceof ApolloError ;
47
58
48
- export const createError = ( name , config ) => {
59
+ export const createError = ( name : string , config : ErrorConfig ) => {
49
60
assert ( isObject ( config ) , 'createError requires a config object as the second parameter' ) ;
50
61
assert ( isString ( config . message ) , 'createError requires a "message" property on the config object passed as the second parameter' ) ;
51
62
const e = ApolloError . bind ( null , name , config ) ;
0 commit comments