Skip to content

Commit a934feb

Browse files
committed
Remove serverResponse field in favor of customData
1 parent df638f8 commit a934feb

File tree

7 files changed

+30
-104
lines changed

7 files changed

+30
-104
lines changed

packages/firebase/index.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7620,12 +7620,7 @@ declare namespace firebase.storage {
76207620
/**
76217621
* An error returned by the Firebase Storage SDK.
76227622
*/
7623-
interface FirebaseStorageError {
7624-
name: string;
7625-
code: string;
7626-
message: string;
7627-
serverResponse: null | string;
7628-
}
7623+
interface FirebaseStorageError extends FirebaseError {}
76297624

76307625
interface StorageObserver<T> {
76317626
next?: NextFn<T> | null;

packages/storage-types/index.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { FirebaseApp } from '@firebase/app-types';
19-
import { CompleteFn, NextFn, Unsubscribe } from '@firebase/util';
19+
import { CompleteFn, FirebaseError, NextFn, Unsubscribe } from '@firebase/util';
2020

2121
export interface FullMetadata extends UploadMetadata {
2222
bucket: string;
@@ -85,12 +85,7 @@ export interface UploadMetadata extends SettableMetadata {
8585
md5Hash?: string | null;
8686
}
8787

88-
export interface FirebaseStorageError {
89-
name: string;
90-
code: string;
91-
message: string;
92-
serverResponse: null | string;
93-
}
88+
export interface FirebaseStorageError extends FirebaseError {}
9489

9590
export interface StorageObserver<T> {
9691
next?: NextFn<T> | null;

packages/storage/src/implementation/error.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FirebaseError } from '@firebase/util';
12
/**
23
* @license
34
* Copyright 2017 Google LLC
@@ -16,53 +17,35 @@
1617
*/
1718
import { CONFIG_STORAGE_BUCKET_KEY } from './constants';
1819

19-
export class FirebaseStorageError implements Error {
20-
private code_: string;
21-
private message_: string;
22-
private serverResponse_: string | null;
23-
private name_: string;
20+
export class FirebaseStorageError extends FirebaseError {
21+
customData: { serverResponse: string | null };
2422

2523
constructor(code: Code, message: string) {
26-
this.code_ = prependCode(code);
27-
this.message_ = 'Firebase Storage: ' + message;
28-
this.serverResponse_ = null;
29-
this.name_ = 'FirebaseError';
30-
}
31-
32-
codeProp(): string {
33-
return this.code;
24+
super(prependCode(code), 'Firebase Storage: ' + message);
25+
// Without this, `instanceof FirebaseStorageError`, in tests for example,
26+
// returns false.
27+
Object.setPrototypeOf(this, FirebaseStorageError.prototype);
28+
this.customData = { serverResponse: null };
3429
}
3530

3631
codeEquals(code: Code): boolean {
37-
return prependCode(code) === this.codeProp();
38-
}
39-
40-
serverResponseProp(): string | null {
41-
return this.serverResponse_;
42-
}
43-
44-
setServerResponseProp(serverResponse: string | null): void {
45-
this.serverResponse_ = serverResponse;
46-
}
47-
48-
get name(): string {
49-
return this.name_;
50-
}
51-
52-
get code(): string {
53-
return this.code_;
32+
return prependCode(code) === this.code;
5433
}
5534

5635
get message(): string {
57-
if (this.serverResponse_) {
58-
return this.message_ + '\n' + this.serverResponse_;
36+
if (this.customData.serverResponse) {
37+
return this.message + '\n' + this.customData.serverResponse;
5938
} else {
60-
return this.message_;
39+
return this.message;
6140
}
6241
}
6342

6443
get serverResponse(): null | string {
65-
return this.serverResponse_;
44+
return this.customData.serverResponse;
45+
}
46+
47+
set serverResponse(serverResponse: string | null) {
48+
this.customData.serverResponse = serverResponse;
6649
}
6750
}
6851

packages/storage/src/implementation/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class NetworkRequest<T> implements Request<T> {
188188
} else {
189189
if (xhr !== null) {
190190
const err = unknown();
191-
err.setServerResponseProp(xhr.getResponseText());
191+
err.serverResponse = xhr.getResponseText();
192192
if (self.errorCallback_) {
193193
reject(self.errorCallback_(xhr, err));
194194
} else {

packages/storage/src/implementation/requests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function sharedErrorHandler(
114114
}
115115
}
116116
}
117-
newErr.setServerResponseProp(err.serverResponseProp());
117+
newErr.serverResponse = err.serverResponse;
118118
return newErr;
119119
}
120120
return errorHandler;
@@ -133,7 +133,7 @@ export function objectErrorHandler(
133133
if (xhr.getStatus() === 404) {
134134
newErr = objectNotFound(location.path);
135135
}
136-
newErr.setServerResponseProp(err.serverResponseProp());
136+
newErr.serverResponse = err.serverResponse;
137137
return newErr;
138138
}
139139
return errorHandler;

packages/util/src/errors.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,16 @@ export interface ErrorData {
6969
[key: string]: unknown;
7070
}
7171

72-
export interface FirebaseError extends Error, ErrorData {
73-
// Unique code for error - format is service/error-code-string.
74-
readonly code: string;
75-
76-
// Developer-friendly error message.
77-
readonly message: string;
78-
79-
// Always 'FirebaseError'.
80-
readonly name: typeof ERROR_NAME;
81-
82-
// Where available - stack backtrace in a string.
83-
readonly stack?: string;
84-
}
85-
8672
// Based on code from:
8773
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types
8874
export class FirebaseError extends Error {
8975
readonly name = ERROR_NAME;
9076

91-
constructor(readonly code: string, message: string) {
77+
constructor(
78+
readonly code: string,
79+
message: string,
80+
public customData?: Record<string, unknown>
81+
) {
9282
super(message);
9383

9484
// Fix For ES5
@@ -125,21 +115,7 @@ export class ErrorFactory<
125115
// Service Name: Error message (service/code).
126116
const fullMessage = `${this.serviceName}: ${message} (${fullCode}).`;
127117

128-
const error = new FirebaseError(fullCode, fullMessage);
129-
130-
// Keys with an underscore at the end of their name are not included in
131-
// error.data for some reason.
132-
// TODO: Replace with Object.entries when lib is updated to es2017.
133-
for (const key of Object.keys(customData)) {
134-
if (key.slice(-1) !== '_') {
135-
if (key in error) {
136-
console.warn(
137-
`Overwriting FirebaseError base field "${key}" can cause unexpected behavior.`
138-
);
139-
}
140-
error[key] = customData[key];
141-
}
142-
}
118+
const error = new FirebaseError(fullCode, fullMessage, customData);
143119

144120
return error;
145121
}

packages/util/test/errors.test.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717
import { assert } from 'chai';
18-
import { stub } from 'sinon';
1918
import { ErrorFactory, ErrorMap, FirebaseError } from '../src/errors';
2019

2120
type ErrorCode =
@@ -60,14 +59,7 @@ describe('FirebaseError', () => {
6059
e.message,
6160
"Fake: Could not find file: 'foo.txt' (fake/file-not-found)."
6261
);
63-
assert.equal(e.file, 'foo.txt');
64-
});
65-
66-
it('anonymously replaces template values with data', () => {
67-
const e = ERROR_FACTORY.create('anon-replace', { repl_: 'world' });
68-
assert.equal(e.code, 'fake/anon-replace');
69-
assert.equal(e.message, 'Fake: Hello, world! (fake/anon-replace).');
70-
assert.isUndefined(e.repl_);
62+
assert.equal(e.customData!.file, 'foo.txt');
7163
});
7264

7365
it('uses "Error" as template when template is missing', () => {
@@ -88,21 +80,6 @@ describe('FirebaseError', () => {
8880
);
8981
});
9082

91-
it('warns if overwriting a base error field with custom data', () => {
92-
const warnStub = stub(console, 'warn');
93-
const e = ERROR_FACTORY.create('overwrite-field', {
94-
code: 'overwritten code'
95-
});
96-
assert.equal(e.code, 'overwritten code');
97-
// TODO: use sinon-chai for this.
98-
assert.ok(
99-
warnStub.calledOnceWith(
100-
'Overwriting FirebaseError base field "code" can cause unexpected behavior.'
101-
)
102-
);
103-
warnStub.restore();
104-
});
105-
10683
it('has stack', () => {
10784
const e = ERROR_FACTORY.create('generic-error');
10885

0 commit comments

Comments
 (0)