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