Skip to content

Commit 1636b5b

Browse files
authored
fix(fcm): Provide inner errors from a session AggregateError to remove ambiguity (#2879)
1 parent 43e5860 commit 1636b5b

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/utils/api-request.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,9 +1345,16 @@ export class Http2SessionHandler {
13451345
})
13461346

13471347
http2Session.on('error', (error) => {
1348+
let errorMessage: any;
1349+
if (error.name == 'AggregateError' && error.errors) {
1350+
errorMessage = `Session error while making requests: ${error.code} - ${error.name}: ` +
1351+
`[${error.errors.map((error: any) => error.message).join(', ')}]`
1352+
} else {
1353+
errorMessage = `Session error while making requests: ${error.code} - ${error.message} `
1354+
}
13481355
this.reject(new FirebaseAppError(
13491356
AppErrorCodes.NETWORK_ERROR,
1350-
`Session error while making requests: ${error}`
1357+
errorMessage
13511358
));
13521359
})
13531360

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function mockHttp2SendRequestError(
140140
} as mocks.MockHttp2Response
141141
}
142142

143-
function mockHttp2Error(streamError?: any, sessionError?:any): mocks.MockHttp2Response {
143+
function mockHttp2Error(streamError: any, sessionError?:any): mocks.MockHttp2Response {
144144
return {
145145
streamError: streamError,
146146
sessionError: sessionError
@@ -2506,10 +2506,10 @@ describe('Http2Client', () => {
25062506
it('should fail on session and stream errors', async () => {
25072507
const reqData = { request: 'data' };
25082508
const streamError = 'Error while making request: test stream error. Error code: AWFUL_STREAM_ERROR';
2509-
const sessionError = 'Session error while making requests: Error: AWFUL_SESSION_ERROR'
2509+
const sessionError = 'Session error while making requests: AWFUL_SESSION_ERROR - test session error'
25102510
mockedHttp2Responses.push(mockHttp2Error(
25112511
{ message: 'test stream error', code: 'AWFUL_STREAM_ERROR' },
2512-
new Error('AWFUL_SESSION_ERROR')
2512+
{ message: 'test session error', code: 'AWFUL_SESSION_ERROR' }
25132513
));
25142514
http2Mocker.http2Stub(mockedHttp2Responses);
25152515

@@ -2540,6 +2540,52 @@ describe('Http2Client', () => {
25402540
await http2SessionHandler.invoke().should.eventually.be.rejectedWith(sessionError)
25412541
.and.have.property('code', 'app/network-error')
25422542
});
2543+
2544+
it('should unwrap aggregate session errors', async () => {
2545+
const reqData = { request: 'data' };
2546+
const streamError = { message: 'test stream error', code: 'AWFUL_STREAM_ERROR' }
2547+
const expectedStreamErrorMessage = 'Error while making request: test stream error. Error code: AWFUL_STREAM_ERROR';
2548+
const aggregateSessionError = {
2549+
name: 'AggregateError',
2550+
code: 'AWFUL_SESSION_ERROR',
2551+
errors: [
2552+
{ message: 'Error message 1' },
2553+
{ message: 'Error message 2' },
2554+
]
2555+
}
2556+
const expectedAggregateErrorMessage = 'Session error while making requests: AWFUL_SESSION_ERROR - ' +
2557+
'AggregateError: [Error message 1, Error message 2]'
2558+
2559+
mockedHttp2Responses.push(mockHttp2Error(streamError, aggregateSessionError));
2560+
http2Mocker.http2Stub(mockedHttp2Responses);
2561+
2562+
const client = new Http2Client();
2563+
http2SessionHandler = new Http2SessionHandler(mockHostUrl)
2564+
2565+
await client.send({
2566+
method: 'POST',
2567+
url: mockUrl,
2568+
headers: {
2569+
'authorization': 'Bearer token',
2570+
'My-Custom-Header': 'CustomValue',
2571+
},
2572+
data: reqData,
2573+
http2SessionHandler: http2SessionHandler,
2574+
}).should.eventually.be.rejectedWith(expectedStreamErrorMessage).and.have.property('code', 'app/network-error')
2575+
.then(() => {
2576+
expect(http2Mocker.requests.length).to.equal(1);
2577+
expect(http2Mocker.requests[0].headers[':method']).to.equal('POST');
2578+
expect(http2Mocker.requests[0].headers[':scheme']).to.equal('https:');
2579+
expect(http2Mocker.requests[0].headers[':path']).to.equal(mockPath);
2580+
expect(JSON.parse(http2Mocker.requests[0].data)).to.deep.equal(reqData);
2581+
expect(http2Mocker.requests[0].headers.authorization).to.equal('Bearer token');
2582+
expect(http2Mocker.requests[0].headers['content-type']).to.contain('application/json');
2583+
expect(http2Mocker.requests[0].headers['My-Custom-Header']).to.equal('CustomValue');
2584+
});
2585+
2586+
await http2SessionHandler.invoke().should.eventually.be.rejectedWith(expectedAggregateErrorMessage)
2587+
.and.have.property('code', 'app/network-error')
2588+
});
25432589
});
25442590

25452591
describe('AuthorizedHttpClient', () => {

0 commit comments

Comments
 (0)