diff --git a/src/utils/api-request.ts b/src/utils/api-request.ts index ca3140a27d..f26fcf9326 100644 --- a/src/utils/api-request.ts +++ b/src/utils/api-request.ts @@ -1345,9 +1345,16 @@ export class Http2SessionHandler { }) http2Session.on('error', (error) => { + let errorMessage: any; + if (error.name == 'AggregateError' && error.errors) { + errorMessage = `Session error while making requests: ${error.code} - ${error.name}: ` + + `[${error.errors.map((error: any) => error.message).join(', ')}]` + } else { + errorMessage = `Session error while making requests: ${error.code} - ${error.message} ` + } this.reject(new FirebaseAppError( AppErrorCodes.NETWORK_ERROR, - `Session error while making requests: ${error}` + errorMessage )); }) diff --git a/test/unit/utils/api-request.spec.ts b/test/unit/utils/api-request.spec.ts index 691f0f89e0..642d011460 100644 --- a/test/unit/utils/api-request.spec.ts +++ b/test/unit/utils/api-request.spec.ts @@ -140,7 +140,7 @@ function mockHttp2SendRequestError( } as mocks.MockHttp2Response } -function mockHttp2Error(streamError?: any, sessionError?:any): mocks.MockHttp2Response { +function mockHttp2Error(streamError: any, sessionError?:any): mocks.MockHttp2Response { return { streamError: streamError, sessionError: sessionError @@ -2506,10 +2506,10 @@ describe('Http2Client', () => { it('should fail on session and stream errors', async () => { const reqData = { request: 'data' }; const streamError = 'Error while making request: test stream error. Error code: AWFUL_STREAM_ERROR'; - const sessionError = 'Session error while making requests: Error: AWFUL_SESSION_ERROR' + const sessionError = 'Session error while making requests: AWFUL_SESSION_ERROR - test session error' mockedHttp2Responses.push(mockHttp2Error( { message: 'test stream error', code: 'AWFUL_STREAM_ERROR' }, - new Error('AWFUL_SESSION_ERROR') + { message: 'test session error', code: 'AWFUL_SESSION_ERROR' } )); http2Mocker.http2Stub(mockedHttp2Responses); @@ -2540,6 +2540,52 @@ describe('Http2Client', () => { await http2SessionHandler.invoke().should.eventually.be.rejectedWith(sessionError) .and.have.property('code', 'app/network-error') }); + + it('should unwrap aggregate session errors', async () => { + const reqData = { request: 'data' }; + const streamError = { message: 'test stream error', code: 'AWFUL_STREAM_ERROR' } + const expectedStreamErrorMessage = 'Error while making request: test stream error. Error code: AWFUL_STREAM_ERROR'; + const aggregateSessionError = { + name: 'AggregateError', + code: 'AWFUL_SESSION_ERROR', + errors: [ + { message: 'Error message 1' }, + { message: 'Error message 2' }, + ] + } + const expectedAggregateErrorMessage = 'Session error while making requests: AWFUL_SESSION_ERROR - ' + + 'AggregateError: [Error message 1, Error message 2]' + + mockedHttp2Responses.push(mockHttp2Error(streamError, aggregateSessionError)); + http2Mocker.http2Stub(mockedHttp2Responses); + + const client = new Http2Client(); + http2SessionHandler = new Http2SessionHandler(mockHostUrl) + + await client.send({ + method: 'POST', + url: mockUrl, + headers: { + 'authorization': 'Bearer token', + 'My-Custom-Header': 'CustomValue', + }, + data: reqData, + http2SessionHandler: http2SessionHandler, + }).should.eventually.be.rejectedWith(expectedStreamErrorMessage).and.have.property('code', 'app/network-error') + .then(() => { + expect(http2Mocker.requests.length).to.equal(1); + expect(http2Mocker.requests[0].headers[':method']).to.equal('POST'); + expect(http2Mocker.requests[0].headers[':scheme']).to.equal('https:'); + expect(http2Mocker.requests[0].headers[':path']).to.equal(mockPath); + expect(JSON.parse(http2Mocker.requests[0].data)).to.deep.equal(reqData); + expect(http2Mocker.requests[0].headers.authorization).to.equal('Bearer token'); + expect(http2Mocker.requests[0].headers['content-type']).to.contain('application/json'); + expect(http2Mocker.requests[0].headers['My-Custom-Header']).to.equal('CustomValue'); + }); + + await http2SessionHandler.invoke().should.eventually.be.rejectedWith(expectedAggregateErrorMessage) + .and.have.property('code', 'app/network-error') + }); }); describe('AuthorizedHttpClient', () => {