@@ -5,94 +5,94 @@ 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
7
export interface ErrorConfig {
8
- message : string ;
9
- time_thrown ?: string ;
10
- data ?: any ;
11
- options ?: {
12
- showPath ?: boolean ;
13
- showLocations ?: boolean ;
14
- } ,
8
+ message : string ;
9
+ time_thrown ?: string ;
10
+ data ?: any ;
11
+ options ?: {
12
+ showPath ?: boolean ;
13
+ showLocations ?: boolean ;
14
+ } ;
15
15
}
16
16
17
17
export interface ErrorInfo {
18
- message : string ;
19
- name : string ;
20
- time_thrown : string ;
21
- data : string ;
22
- path : string ;
23
- locations : string ;
18
+ message : string ;
19
+ name : string ;
20
+ time_thrown : string ;
21
+ data : string ;
22
+ path : string ;
23
+ locations : string ;
24
24
}
25
25
26
26
export class ApolloError extends ExtendableError {
27
- name : string ;
28
- message : string ;
29
- time_thrown : string ;
30
- data : any ;
31
- path : any ;
32
- locations : any ;
33
- _showLocations : boolean = false ;
34
-
35
- constructor ( name :string , config : ErrorConfig ) {
36
- super ( ( arguments [ 2 ] && arguments [ 2 ] . message ) || '' ) ;
37
-
38
- const t = ( arguments [ 2 ] && arguments [ 2 ] . time_thrown ) || ( new Date ( ) ) . toISOString ( ) ;
39
- const m = ( arguments [ 2 ] && arguments [ 2 ] . message ) || '' ;
40
- const configData = ( arguments [ 2 ] && arguments [ 2 ] . data ) || { } ;
41
- const d = { ...this . data , ...configData }
42
- const opts = ( ( arguments [ 2 ] && arguments [ 2 ] . options ) || { } )
43
-
44
- this . name = name ;
45
- this . message = m ;
46
- this . time_thrown = t ;
47
- this . data = d ;
48
- this . _showLocations = ! ! opts . showLocations ;
49
- }
50
-
51
- serialize ( ) : ErrorInfo {
52
- const { name, message, time_thrown, data, _showLocations, path, locations } = this ;
53
-
54
- let error : ErrorInfo = {
55
- message,
56
- name,
57
- time_thrown,
58
- data,
59
- path,
60
- locations
61
- } ;
62
-
63
- if ( _showLocations ) {
64
- error . locations = locations ;
65
- error . path = path ;
66
- }
67
-
68
- return error ;
69
- }
27
+ name : string ;
28
+ message : string ;
29
+ time_thrown : string ;
30
+ data : any ;
31
+ path : any ;
32
+ locations : any ;
33
+ _showLocations : boolean = false ;
34
+
35
+ constructor ( name : string , config : ErrorConfig ) {
36
+ super ( ( arguments [ 2 ] && arguments [ 2 ] . message ) || '' ) ;
37
+
38
+ const t = ( arguments [ 2 ] && arguments [ 2 ] . time_thrown ) || ( new Date ( ) ) . toISOString ( ) ;
39
+ const m = ( arguments [ 2 ] && arguments [ 2 ] . message ) || '' ;
40
+ const configData = ( arguments [ 2 ] && arguments [ 2 ] . data ) || { } ;
41
+ const d = { ...this . data , ...configData }
42
+ const opts = ( ( arguments [ 2 ] && arguments [ 2 ] . options ) || { } )
43
+
44
+ this . name = name ;
45
+ this . message = m ;
46
+ this . time_thrown = t ;
47
+ this . data = d ;
48
+ this . _showLocations = ! ! opts . showLocations ;
49
+ }
50
+
51
+ serialize ( ) : ErrorInfo {
52
+ const { name, message, time_thrown, data, _showLocations, path, locations } = this ;
53
+
54
+ let error : ErrorInfo = {
55
+ message,
56
+ name,
57
+ time_thrown,
58
+ data,
59
+ path,
60
+ locations
61
+ } ;
62
+
63
+ if ( _showLocations ) {
64
+ error . locations = locations ;
65
+ error . path = path ;
66
+ }
67
+
68
+ return error ;
69
+ }
70
70
}
71
71
72
72
export const isInstance = e => e instanceof ApolloError ;
73
73
74
- export const createError = ( name :string , config : ErrorConfig ) => {
75
- assert ( isObject ( config ) , 'createError requires a config object as the second parameter' ) ;
76
- assert ( isString ( config . message ) , 'createError requires a "message" property on the config object passed as the second parameter' ) ;
77
- return new ApolloError ( name , config ) ;
74
+ export const createError = ( name : string , config : ErrorConfig ) => {
75
+ assert ( isObject ( config ) , 'createError requires a config object as the second parameter' ) ;
76
+ assert ( isString ( config . message ) , 'createError requires a "message" property on the config object passed as the second parameter' ) ;
77
+ return new ApolloError ( name , config ) ;
78
78
} ;
79
79
80
80
export const formatError = ( error , returnNull = false ) : ErrorInfo => {
81
- const originalError = error ? error . originalError || error : null ;
81
+ const originalError = error ? error . originalError || error : null ;
82
82
83
- if ( ! originalError ) return returnNull ? null : error ;
83
+ if ( ! originalError ) return returnNull ? null : error ;
84
84
85
- const { name } = originalError ;
85
+ const { name } = originalError ;
86
86
87
- if ( ! name || ! isInstance ( originalError ) ) return returnNull ? null : error ;
87
+ if ( ! name || ! isInstance ( originalError ) ) return returnNull ? null : error ;
88
88
89
- const { time_thrown, message, data, _showLocations } = originalError ;
89
+ const { time_thrown, message, data, _showLocations } = originalError ;
90
90
91
- if ( _showLocations ) {
92
- const { locations, path } = error ;
93
- originalError . locations = locations ;
94
- originalError . path = path ;
95
- }
91
+ if ( _showLocations ) {
92
+ const { locations, path } = error ;
93
+ originalError . locations = locations ;
94
+ originalError . path = path ;
95
+ }
96
96
97
- return originalError . serialize ( ) ;
97
+ return originalError . serialize ( ) ;
98
98
} ;
0 commit comments