Skip to content

Commit f08bedb

Browse files
committed
Promise based option
1 parent 2a9dce8 commit f08bedb

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

src/messaging/messaging.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,22 @@ export class Messaging {
206206
MessagingClientErrorCode.INVALID_ARGUMENT, 'dryRun must be a boolean');
207207
}
208208

209-
const http2SessionHandler = this.useLegacyTransport ? undefined : new Http2SessionHandler(`https://${FCM_SEND_HOST}`)
209+
// const http2SessionHandler = this.useLegacyTransport ? undefined : new Http2SessionHandler(`https://${FCM_SEND_HOST}`)
210+
const http2SessionHandler = this.useLegacyTransport ? undefined : new Http2SessionHandler(`https://localhost:3001`);
210211

211212
return this.getUrlPath()
212213
.then((urlPath) => {
214+
// Try listening for errors here?
215+
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
222+
})
223+
}
224+
213225
const requests: Promise<SendResponse>[] = copy.map(async (message) => {
214226
validateMessage(message);
215227
const request: { message: Message; validate_only?: boolean } = { message };

src/utils/api-request.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,12 +847,19 @@ class AsyncHttp2Call extends AsyncRequestCall {
847847
...this.options.headers
848848
});
849849

850+
// console.log("EMIT SESSION ERROR")
851+
// this.http2ConfigImpl.http2SessionHandler.session.emit('error', "MOCK_SESSION_ERROR")
852+
850853
req.on('response', (headers: IncomingHttp2Headers) => {
851854
this.handleHttp2Response(headers, req);
855+
856+
// console.log("EMIT SESSION ERROR")
857+
// this.http2ConfigImpl.http2SessionHandler.session.emit('error', "MOCK_ERROR")
852858
});
853859

854860
// Handle errors
855861
req.on('error', (err: any) => {
862+
console.log("GOT REQUEST ERROR")
856863
if (req.aborted) {
857864
return;
858865
}
@@ -1315,9 +1322,16 @@ export class ExponentialBackoffPoller<T> extends EventEmitter {
13151322
export class Http2SessionHandler {
13161323

13171324
private http2Session: http2.ClientHttp2Session
1325+
protected promise: Promise<void>
1326+
protected resolve: () => void;
1327+
protected reject: (_: any) => void;
13181328

13191329
constructor(url: string){
1320-
this.http2Session = this.createSession(url)
1330+
this.promise = new Promise((resolve, reject) => {
1331+
this.resolve = resolve;
1332+
this.reject = reject;
1333+
this.http2Session = this.createSession(url)
1334+
});
13211335
}
13221336

13231337
public createSession(url: string): http2.ClientHttp2Session {
@@ -1330,23 +1344,37 @@ export class Http2SessionHandler {
13301344
const http2Session = http2.connect(url, opts)
13311345

13321346
http2Session.on('goaway', (errorCode, _, opaqueData) => {
1333-
throw new FirebaseAppError(
1347+
console.log("GOT SESSION GOAWAY EVENT")
1348+
this.reject(new FirebaseAppError(
13341349
AppErrorCodes.NETWORK_ERROR,
13351350
`Error while making requests: GOAWAY - ${opaqueData.toString()}, Error code: ${errorCode}`
1336-
);
1351+
));
13371352
})
13381353

13391354
http2Session.on('error', (error) => {
1340-
throw new FirebaseAppError(
1355+
console.log("GOT SESSION ERROR EVENT")
1356+
this.reject(new FirebaseAppError(
13411357
AppErrorCodes.NETWORK_ERROR,
13421358
`Error while making requests: ${error}`
1343-
);
1359+
));
13441360
})
1361+
1362+
// Session close should be where we resolve the promise since we no longer need to listen for errors
1363+
http2Session.on('close', () => {
1364+
console.log("GOT SESSION CLOSE EVENT")
1365+
this.resolve()
1366+
});
1367+
13451368
return http2Session
13461369
}
13471370
return this.http2Session
13481371
}
13491372

1373+
// return the promise tracking events
1374+
public invoke(): Promise<void> {
1375+
return this.promise
1376+
}
1377+
13501378
get session(): http2.ClientHttp2Session {
13511379
return this.http2Session
13521380
}

test/integration/messaging.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ describe('admin.messaging', () => {
101101
});
102102
});
103103

104+
it('test uncaught error', () => {
105+
const messages: Message[] = [message, message, message];
106+
// No longer throws uncatchable error
107+
return getMessaging().sendEach(messages, true)
108+
.then((response) => {
109+
console.log(response.responses)
110+
})
111+
.catch((error) => {
112+
// This error isn't passed here even when thrown
113+
console.log("CAUGHT ERROR")
114+
console.log(error)
115+
})
116+
});
117+
104118
it('sendEach(500)', () => {
105119
const messages: Message[] = [];
106120
for (let i = 0; i < 500; i++) {

0 commit comments

Comments
 (0)