@@ -28,26 +28,50 @@ describe('ParseError', () => {
2828 } ) ;
2929 } ) ;
3030
31- it ( 'message must be a string' , ( ) => {
32- /**
33- * error as object
34- */
35- const someRandomError = { code : 420 , message : 'time to chill' } ;
31+ it ( 'message can be a string' , ( ) => {
32+ const someRandomError = 'oh no' ;
33+
34+ const error = new ParseError ( 1337 , someRandomError ) ;
35+
36+ expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
37+ message : someRandomError ,
38+ code : 1337 ,
39+ } ) ;
40+ } ) ;
41+
42+ it ( 'message can be an object passed trough some external dependency' , ( ) => {
43+ const someRandomError = { code : '420' , message : 'time to chill' , status : '🎮' } ;
44+
3645 const error = new ParseError ( 1337 , someRandomError ) ;
46+
3747 expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
38- message : JSON . stringify ( someRandomError ) ,
48+ message : '420 time to chill 🎮' ,
3949 code : 1337 ,
4050 } ) ;
51+ } ) ;
4152
42- /**
43- * error as an Error instance
44- */
45- const someRandomError2 = new Error ( 'time to relax' ) ;
46- const error2 = new ParseError ( 420 , someRandomError2 ) ;
53+ it ( 'message can be an Error instance *receiving a string* passed trough some external dependency' , ( ) => {
54+ const someRandomError = new Error ( 'good point' ) ;
4755
48- expect ( JSON . parse ( JSON . stringify ( error2 ) ) ) . toEqual ( {
49- message : 'Error: time to relax' ,
50- code : 420 ,
56+ const error = new ParseError ( 1337 , someRandomError ) ;
57+
58+ expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
59+ message : 'Error: good point' ,
60+ code : 1337 ,
61+ } ) ;
62+ } ) ;
63+
64+ it ( 'message can be an Error instance *receiving an object* passed trough some external dependency' , ( ) => {
65+ const someRandomErrorWrong = new Error ( {
66+ code : 'WRONG' ,
67+ message : 'this is not how errors should be handled' ,
68+ } ) ;
69+
70+ const error = new ParseError ( 1337 , someRandomErrorWrong ) ;
71+
72+ expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
73+ message : '' , // <-- Yeah because we can't parse errors used like that
74+ code : 1337 ,
5175 } ) ;
5276 } ) ;
5377} ) ;
0 commit comments