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 {
2222 this . code = code ;
2323 Object . defineProperty ( this , 'message' , {
2424 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 ) ,
2633 } ) ;
2734 }
2835
Original file line number Diff line number Diff line change @@ -29,11 +29,25 @@ describe('ParseError', () => {
2929 } ) ;
3030
3131 it ( 'message must be a string' , ( ) => {
32+ /**
33+ * error as object
34+ */
3235 const someRandomError = { code : 420 , message : 'time to chill' } ;
3336 const error = new ParseError ( 1337 , someRandomError ) ;
3437 expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
3538 message : JSON . stringify ( someRandomError ) ,
3639 code : 1337 ,
3740 } ) ;
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+ } ) ;
3852 } ) ;
3953} ) ;
You can’t perform that action at this time.
0 commit comments