@@ -28,11 +28,49 @@ describe('ParseError', () => {
28
28
} ) ;
29
29
} ) ;
30
30
31
- it ( 'message must be a string' , ( ) => {
32
- 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
+
45
+ const error = new ParseError ( 1337 , someRandomError ) ;
46
+
47
+ expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
48
+ message : '420 time to chill 🎮' ,
49
+ code : 1337 ,
50
+ } ) ;
51
+ } ) ;
52
+
53
+ it ( 'message can be an Error instance *receiving a string* passed trough some external dependency' , ( ) => {
54
+ const someRandomError = new Error ( 'good point' ) ;
55
+
33
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
+
34
72
expect ( JSON . parse ( JSON . stringify ( error ) ) ) . toEqual ( {
35
- message : JSON . stringify ( someRandomError ) ,
73
+ message : '' , // <-- Yeah because we can't parse errors used like that
36
74
code : 1337 ,
37
75
} ) ;
38
76
} ) ;
0 commit comments