Skip to content

Commit e0b87ae

Browse files
committed
Add types to ErrorFactory message parameters
1 parent 0acd32c commit e0b87ae

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

packages/util/src/errors.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,25 @@ export class FirebaseError extends Error {
103103
}
104104
}
105105

106-
export class ErrorFactory<ErrorCode extends string> {
106+
export class ErrorFactory<
107+
ErrorCode extends string,
108+
ErrorParams extends { readonly [K in ErrorCode]?: ErrorData } = {}
109+
> {
107110
constructor(
108111
private readonly service: string,
109112
private readonly serviceName: string,
110113
private readonly errors: ErrorMap<ErrorCode>
111114
) {}
112115

113-
create(code: ErrorCode, data: ErrorData = {}): FirebaseError {
116+
create<K extends ErrorCode>(
117+
code: K,
118+
...data: K extends keyof ErrorParams ? [ErrorParams[K]] : []
119+
): FirebaseError {
120+
const customData = data[0] || {};
114121
const fullCode = `${this.service}/${code}`;
115122
const template = this.errors[code];
116123

117-
const message = template ? replaceTemplate(template, data) : 'Error';
124+
const message = template ? replaceTemplate(template, customData) : 'Error';
118125
// Service Name: Error message (service/code).
119126
const fullMessage = `${this.serviceName}: ${message} (${fullCode}).`;
120127

@@ -123,14 +130,14 @@ export class ErrorFactory<ErrorCode extends string> {
123130
// Keys with an underscore at the end of their name are not included in
124131
// error.data for some reason.
125132
// TODO: Replace with Object.entries when lib is updated to es2017.
126-
for (const key of Object.keys(data)) {
133+
for (const key of Object.keys(customData)) {
127134
if (key.slice(-1) !== '_') {
128135
if (key in error) {
129136
console.warn(
130137
`Overwriting FirebaseError base field "${key}" can cause unexpected behavior.`
131138
);
132139
}
133-
error[key] = data[key];
140+
error[key] = customData[key];
134141
}
135142
}
136143

packages/util/test/errors.test.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@ const ERROR_MAP: ErrorMap<ErrorCode> = {
3232
'I decided to use {$code} to represent the error code from my server.'
3333
};
3434

35-
const ERROR_FACTORY = new ErrorFactory<ErrorCode>('fake', 'Fake', ERROR_MAP);
35+
interface ErrorParams {
36+
'file-not-found': { file: string };
37+
'anon-replace': { repl_: string };
38+
'overwrite-field': { code: string };
39+
}
40+
41+
const ERROR_FACTORY = new ErrorFactory<ErrorCode, ErrorParams>(
42+
'fake',
43+
'Fake',
44+
ERROR_MAP
45+
);
3646

3747
describe('FirebaseError', () => {
3848
it('creates an Error', () => {
@@ -68,7 +78,9 @@ describe('FirebaseError', () => {
6878
});
6979

7080
it('uses the key in the template if the replacement is missing', () => {
71-
const e = ERROR_FACTORY.create('file-not-found', { fileX: 'foo.txt' });
81+
const e = ERROR_FACTORY.create('file-not-found', {
82+
fileX: 'foo.txt'
83+
} as any);
7284
assert.equal(e.code, 'fake/file-not-found');
7385
assert.equal(
7486
e.message,

0 commit comments

Comments
 (0)