Skip to content

Commit 7a7faa6

Browse files
committed
Wrap session errors in a new FirebaseMessagingSessionError
1 parent 7f86834 commit 7a7faa6

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

etc/firebase-admin.app.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export class AppErrorCodes {
1919
// (undocumented)
2020
static DUPLICATE_APP: string;
2121
// (undocumented)
22-
static HTTP2_SESSION_ERROR: string;
23-
// (undocumented)
2422
static INTERNAL_ERROR: string;
2523
// (undocumented)
2624
static INVALID_APP_NAME: string;

src/messaging/messaging.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import { App } from '../app';
1919
import { deepCopy } from '../utils/deep-copy';
20-
import { ErrorInfo, MessagingClientErrorCode, FirebaseMessagingError } from '../utils/error';
20+
import {
21+
ErrorInfo, MessagingClientErrorCode, FirebaseMessagingError, FirebaseMessagingSessionError
22+
} from '../utils/error';
2123
import * as utils from '../utils';
2224
import * as validator from '../utils/validator';
2325
import { validateMessage } from './messaging-internal';
@@ -211,13 +213,13 @@ export class Messaging {
211213
return this.getUrlPath()
212214
.then((urlPath) => {
213215
if (http2SessionHandler) {
214-
let batchResponsePromise: Promise<PromiseSettledResult<SendResponse>[]>;
216+
let sendResponsePromise: Promise<PromiseSettledResult<SendResponse>[]>;
215217
return new Promise((resolve: (result: PromiseSettledResult<SendResponse>[]) => void, reject) => {
216218
// Start session listeners
217219
http2SessionHandler.invoke().catch((error) => {
218-
error.pendingBatchResponse =
219-
batchResponsePromise ? batchResponsePromise.then(this.parseSendResponses) : undefined;
220-
reject(error);
220+
const pendingBatchResponse =
221+
sendResponsePromise ? sendResponsePromise.then(this.parseSendResponses) : undefined;
222+
reject(new FirebaseMessagingSessionError(error, undefined, pendingBatchResponse));
221223
});
222224

223225
// Start making requests
@@ -232,8 +234,8 @@ export class Messaging {
232234
});
233235

234236
// Resolve once all requests have completed
235-
batchResponsePromise = Promise.allSettled(requests);
236-
batchResponsePromise.then(resolve);
237+
sendResponsePromise = Promise.allSettled(requests);
238+
sendResponsePromise.then(resolve);
237239
});
238240
} else {
239241
const requests: Promise<SendResponse>[] = copy.map(async (message) => {

src/utils/api-request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,14 +1339,14 @@ export class Http2SessionHandler {
13391339

13401340
http2Session.on('goaway', (errorCode, _, opaqueData) => {
13411341
this.reject(new FirebaseAppError(
1342-
AppErrorCodes.HTTP2_SESSION_ERROR,
1342+
AppErrorCodes.NETWORK_ERROR,
13431343
`Error while making requests: GOAWAY - ${opaqueData?.toString()}, Error code: ${errorCode}`
13441344
));
13451345
})
13461346

13471347
http2Session.on('error', (error) => {
13481348
this.reject(new FirebaseAppError(
1349-
AppErrorCodes.HTTP2_SESSION_ERROR,
1349+
AppErrorCodes.NETWORK_ERROR,
13501350
`Session error while making requests: ${error}`
13511351
));
13521352
})

src/utils/error.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { FirebaseError as FirebaseErrorInterface } from '../app';
19+
import { BatchResponse } from '../messaging/messaging-api';
1920
import { deepCopy } from '../utils/deep-copy';
2021

2122
/**
@@ -344,6 +345,38 @@ export class FirebaseMessagingError extends PrefixedFirebaseError {
344345
}
345346
}
346347

348+
export class FirebaseMessagingSessionError extends FirebaseMessagingError {
349+
public pendingBatchResponse?: Promise<BatchResponse>;
350+
/**
351+
*
352+
* @param info - The error code info.
353+
* @param message - The error message. This will override the default message if provided.
354+
* @param pendingBatchResponse - BatchResponse for pending messages when session error occured.
355+
* @constructor
356+
* @internal
357+
*/
358+
constructor(info: ErrorInfo, message?: string, pendingBatchResponse?: Promise<BatchResponse>) {
359+
// Override default message if custom message provided.
360+
super(info, message || info.message);
361+
this.pendingBatchResponse = pendingBatchResponse;
362+
363+
/* tslint:disable:max-line-length */
364+
// Set the prototype explicitly. See the following link for more details:
365+
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
366+
/* tslint:enable:max-line-length */
367+
(this as any).__proto__ = FirebaseMessagingSessionError.prototype;
368+
}
369+
370+
/** @returns The object representation of the error. */
371+
public toJSON(): object {
372+
return {
373+
code: this.code,
374+
message: this.message,
375+
pendingBatchResponse: this.pendingBatchResponse,
376+
};
377+
}
378+
}
379+
347380
/**
348381
* Firebase project management error code structure. This extends PrefixedFirebaseError.
349382
*/
@@ -377,7 +410,6 @@ export class AppErrorCodes {
377410
public static INVALID_APP_OPTIONS = 'invalid-app-options';
378411
public static INVALID_CREDENTIAL = 'invalid-credential';
379412
public static NETWORK_ERROR = 'network-error';
380-
public static HTTP2_SESSION_ERROR = 'http2-session-error';
381413
public static NETWORK_TIMEOUT = 'network-timeout';
382414
public static NO_APP = 'no-app';
383415
public static UNABLE_TO_PARSE_RESPONSE = 'unable-to-parse-response';

test/unit/messaging/messaging.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import { HttpClient } from '../../../src/utils/api-request';
3535
import { getMetricsHeader, getSdkVersion } from '../../../src/utils/index';
3636
import * as utils from '../utils';
37+
import { FirebaseMessagingSessionError } from '../../../src/utils/error';
3738

3839
chai.should();
3940
chai.use(sinonChai);
@@ -923,9 +924,10 @@ describe('Messaging', () => {
923924

924925
return messaging.sendEach(
925926
[validMessage, validMessage], true
926-
).catch(async (error) => {
927-
expect(error.errorInfo.code).to.equal('app/http2-session-error')
928-
await error.pendingBatchResponse.then((response: BatchResponse) => {
927+
).catch(async (error: FirebaseMessagingSessionError) => {
928+
expect(error.code).to.equal('messaging/app/network-error');
929+
expect(error.pendingBatchResponse).to.not.be.undefined;
930+
await error.pendingBatchResponse?.then((response: BatchResponse) => {
929931
expect(http2Mocker.requests.length).to.equal(2);
930932
expect(response.failureCount).to.equal(1);
931933
const responses = response.responses;

test/unit/utils/api-request.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,7 @@ describe('Http2Client', () => {
25382538
});
25392539

25402540
await http2SessionHandler.invoke().should.eventually.be.rejectedWith(sessionError)
2541+
.and.have.property('code', 'app/network-error')
25412542
});
25422543
});
25432544

0 commit comments

Comments
 (0)