15
15
* limitations under the License.
16
16
*/
17
17
import { assert } from 'chai' ;
18
+ import { stub } from 'sinon' ;
18
19
import { ErrorFactory , ErrorList , FirebaseError } from '../src/errors' ;
19
20
20
- type Err = 'generic-error' | 'file-not-found' | 'anon-replace' ;
21
+ type ErrorCode =
22
+ | 'generic-error'
23
+ | 'file-not-found'
24
+ | 'anon-replace'
25
+ | 'overwrite-field' ;
21
26
22
- let errors : ErrorList < Err > = {
27
+ const ERROR_MAP : ErrorList < ErrorCode > = {
23
28
'generic-error' : 'Unknown error' ,
24
29
'file-not-found' : "Could not find file: '{$file}'" ,
25
- 'anon-replace' : 'Hello, {$repl_}!'
30
+ 'anon-replace' : 'Hello, {$repl_}!' ,
31
+ 'overwrite-field' :
32
+ 'I decided to use {$code} to represent the error code from my server.'
26
33
} ;
27
34
28
- let error = new ErrorFactory < Err > ( 'fake' , 'Fake' , errors ) ;
35
+ const ERROR_FACTORY = new ErrorFactory < ErrorCode > ( 'fake' , 'Fake' , ERROR_MAP ) ;
29
36
30
37
describe ( 'FirebaseError' , ( ) => {
31
38
it ( 'creates an Error' , ( ) => {
32
- let e = error . create ( 'generic-error' ) ;
39
+ const e = ERROR_FACTORY . create ( 'generic-error' ) ;
33
40
assert . instanceOf ( e , Error ) ;
34
41
assert . instanceOf ( e , FirebaseError ) ;
35
42
assert . equal ( e . code , 'fake/generic-error' ) ;
36
43
assert . equal ( e . message , 'Fake: Unknown error (fake/generic-error).' ) ;
37
44
} ) ;
38
45
39
46
it ( 'replaces template values with data' , ( ) => {
40
- let e = error . create ( 'file-not-found' , { file : 'foo.txt' } ) ;
47
+ const e = ERROR_FACTORY . create ( 'file-not-found' , { file : 'foo.txt' } ) ;
41
48
assert . equal ( e . code , 'fake/file-not-found' ) ;
42
49
assert . equal (
43
50
e . message ,
44
51
"Fake: Could not find file: 'foo.txt' (fake/file-not-found)."
45
52
) ;
46
- assert . equal ( e . data . file , 'foo.txt' ) ;
53
+ assert . equal ( e . file , 'foo.txt' ) ;
47
54
} ) ;
48
55
49
56
it ( 'anonymously replaces template values with data' , ( ) => {
50
- let e = error . create ( 'anon-replace' , { repl_ : 'world' } ) ;
57
+ const e = ERROR_FACTORY . create ( 'anon-replace' , { repl_ : 'world' } ) ;
51
58
assert . equal ( e . code , 'fake/anon-replace' ) ;
52
59
assert . equal ( e . message , 'Fake: Hello, world! (fake/anon-replace).' ) ;
53
- assert . isUndefined ( e . data . repl_ ) ;
60
+ assert . isUndefined ( e . repl_ ) ;
54
61
} ) ;
55
62
56
63
it ( 'uses "Error" as template when template is missing' , ( ) => {
57
64
// Cast to avoid compile-time error.
58
- let e = error . create ( ( 'no-such-code' as any ) as Err ) ;
65
+ const e = ERROR_FACTORY . create ( ( 'no-such-code' as any ) as ErrorCode ) ;
59
66
assert . equal ( e . code , 'fake/no-such-code' ) ;
60
67
assert . equal ( e . message , 'Fake: Error (fake/no-such-code).' ) ;
61
68
} ) ;
62
69
63
70
it ( 'uses the key in the template if the replacement is missing' , ( ) => {
64
- let e = error . create ( 'file-not-found' , { fileX : 'foo.txt' } ) ;
71
+ const e = ERROR_FACTORY . create ( 'file-not-found' , { fileX : 'foo.txt' } ) ;
65
72
assert . equal ( e . code , 'fake/file-not-found' ) ;
66
73
assert . equal (
67
74
e . message ,
68
75
"Fake: Could not find file: '<file?>' (fake/file-not-found)."
69
76
) ;
70
77
} ) ;
71
78
79
+ it ( 'warns if overwriting a base error field with custom data' , ( ) => {
80
+ const warnStub = stub ( console , 'warn' ) ;
81
+ const e = ERROR_FACTORY . create ( 'overwrite-field' , {
82
+ code : 'overwritten code'
83
+ } ) ;
84
+ assert . equal ( e . code , 'overwritten code' ) ;
85
+ // TODO: use sinon-chai for this.
86
+ assert . ok (
87
+ warnStub . calledOnceWith (
88
+ 'Overwriting FirebaseError base field "code" can cause unexpected behavior.'
89
+ )
90
+ ) ;
91
+ warnStub . restore ( ) ;
92
+ } ) ;
93
+
72
94
it ( 'has stack' , ( ) => {
73
- let e = error . create ( 'generic-error' ) ;
95
+ const e = ERROR_FACTORY . create ( 'generic-error' ) ;
74
96
// Multi-line match trick - .* does not match \n
75
97
assert . match ( e . stack , / F i r e b a s e E r r o r [ \s \S ] / ) ;
76
98
} ) ;
@@ -91,6 +113,6 @@ function dummy1() {
91
113
}
92
114
93
115
function dummy2 ( ) {
94
- let error = new ErrorFactory < Err > ( 'dummy' , 'Dummy' , errors ) ;
116
+ const error = new ErrorFactory < ErrorCode > ( 'dummy' , 'Dummy' , ERROR_MAP ) ;
95
117
throw error . create ( 'generic-error' ) ;
96
118
}
0 commit comments