@@ -28,26 +28,50 @@ describe('ParseError', () => {
28
28
} ) ;
29
29
} ) ;
30
30
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
+
36
45
const error = new ParseError ( 1337 , someRandomError ) ;
46
+
37
47
expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
38
- message : JSON . stringify ( someRandomError ) ,
48
+ message : '420 time to chill 🎮' ,
39
49
code : 1337 ,
40
50
} ) ;
51
+ } ) ;
41
52
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' ) ;
47
55
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 ,
51
75
} ) ;
52
76
} ) ;
53
77
} ) ;
0 commit comments