Skip to content

Commit 690660f

Browse files
committed
Throw session errors and bundle with a promise to a BatchResponse if one exists
1 parent f08bedb commit 690660f

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/messaging/messaging.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,33 +209,48 @@ export class Messaging {
209209
// const http2SessionHandler = this.useLegacyTransport ? undefined : new Http2SessionHandler(`https://${FCM_SEND_HOST}`)
210210
const http2SessionHandler = this.useLegacyTransport ? undefined : new Http2SessionHandler(`https://localhost:3001`);
211211

212+
212213
return this.getUrlPath()
213214
.then((urlPath) => {
214-
// Try listening for errors here?
215+
// Try listening for errors here?
215216
if (http2SessionHandler){
216-
http2SessionHandler.invoke().catch((error) => {
217-
console.log("ERROR TO BE PASSED TO USER:")
218-
console.log(error)
219-
220-
// Throwing here does nothing since it's still not in the promise that's returned?
221-
throw error
217+
let batchResponsePromise: Promise<PromiseSettledResult<SendResponse>[]>
218+
return new Promise((resolve: (result: PromiseSettledResult<SendResponse>[]) => void, reject) => {
219+
http2SessionHandler.invoke().catch((error) => {
220+
console.log("ERROR TO BE PASSED TO USER:")
221+
console.log(error)
222+
reject({error, batchResponsePromise})
223+
})
224+
225+
226+
const requests: Promise<SendResponse>[] = copy.map(async (message) => {
227+
validateMessage(message);
228+
const request: { message: Message; validate_only?: boolean } = { message };
229+
if (dryRun) {
230+
request.validate_only = true;
231+
}
232+
return this.messagingRequestHandler.invokeHttp2RequestHandlerForSendResponse(
233+
FCM_SEND_HOST, urlPath, request, http2SessionHandler);
234+
});
235+
batchResponsePromise = Promise.allSettled(requests)
236+
batchResponsePromise.then(resolve).catch((error) => {
237+
reject({error, batchResponsePromise})
238+
})
222239
})
223240
}
224241

242+
//
225243
const requests: Promise<SendResponse>[] = copy.map(async (message) => {
226244
validateMessage(message);
227245
const request: { message: Message; validate_only?: boolean } = { message };
228246
if (dryRun) {
229247
request.validate_only = true;
230248
}
231-
232-
if (http2SessionHandler){
233-
return this.messagingRequestHandler.invokeHttp2RequestHandlerForSendResponse(
234-
FCM_SEND_HOST, urlPath, request, http2SessionHandler);
235-
}
236249
return this.messagingRequestHandler.invokeHttpRequestHandlerForSendResponse(FCM_SEND_HOST, urlPath, request);
237250
});
238251
return Promise.allSettled(requests);
252+
//
253+
239254
})
240255
.then((results) => {
241256
const responses: SendResponse[] = [];
@@ -253,11 +268,6 @@ export class Messaging {
253268
failureCount: responses.length - successCount,
254269
};
255270
})
256-
.finally(() => {
257-
if (http2SessionHandler){
258-
http2SessionHandler.close()
259-
}
260-
});
261271
}
262272

263273
/**

test/integration/messaging.spec.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,26 @@ describe('admin.messaging', () => {
101101
});
102102
});
103103

104-
it('test uncaught error', () => {
104+
it('test uncaught error', async () => {
105105
const messages: Message[] = [message, message, message];
106+
106107
// No longer throws uncatchable error
107108
return getMessaging().sendEach(messages, true)
108109
.then((response) => {
109110
console.log(response.responses)
110111
})
111-
.catch((error) => {
112-
// This error isn't passed here even when thrown
112+
.catch(async (error) => {
113+
// type err = {
114+
// error: any,
115+
// batchResponsePromise: Promise<PromiseSettledResult<SendResponse>[]>
116+
// }
117+
// If batchResponsePromise is undefined then no messages were sent
118+
// The promise should eventally return with the result of each request
113119
console.log("CAUGHT ERROR")
114120
console.log(error)
121+
await error.batchResponsePromise.then((results: any) => {
122+
console.log(results)
123+
})
115124
})
116125
});
117126

0 commit comments

Comments
 (0)