File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,14 @@ class ParseError extends Error {
22
22
this . code = code ;
23
23
Object . defineProperty ( this , 'message' , {
24
24
enumerable : true ,
25
- value : typeof message === 'string' ? message : JSON . stringify ( message ) ,
25
+ value :
26
+ typeof message === 'string'
27
+ ? message
28
+ : typeof message === 'object' &&
29
+ typeof message . toString === 'function' &&
30
+ message . toString ( ) !== '[object Object]'
31
+ ? message . toString ( )
32
+ : JSON . stringify ( message ) ,
26
33
} ) ;
27
34
}
28
35
Original file line number Diff line number Diff line change @@ -29,11 +29,25 @@ describe('ParseError', () => {
29
29
} ) ;
30
30
31
31
it ( 'message must be a string' , ( ) => {
32
+ /**
33
+ * error as object
34
+ */
32
35
const someRandomError = { code : 420 , message : 'time to chill' } ;
33
36
const error = new ParseError ( 1337 , someRandomError ) ;
34
37
expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
35
38
message : JSON . stringify ( someRandomError ) ,
36
39
code : 1337 ,
37
40
} ) ;
41
+
42
+ /**
43
+ * error as an Error instance
44
+ */
45
+ const someRandomError2 = new Error ( 'time to relax' ) ;
46
+ const error2 = new ParseError ( 420 , someRandomError2 ) ;
47
+
48
+ expect ( JSON . parse ( JSON . stringify ( error2 ) ) ) . toEqual ( {
49
+ message : 'Error: time to relax' ,
50
+ code : 420 ,
51
+ } ) ;
38
52
} ) ;
39
53
} ) ;
You can’t perform that action at this time.
0 commit comments