Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/utils/api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
})

Expand Down
52 changes: 49 additions & 3 deletions test/unit/utils/api-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
} as mocks.MockHttp2Response
}

function mockHttp2Error(streamError?: any, sessionError?:any): mocks.MockHttp2Response {
function mockHttp2Error(streamError: any, sessionError?:any): mocks.MockHttp2Response {
return {
streamError: streamError,
sessionError: sessionError
Expand Down Expand Up @@ -2506,10 +2506,10 @@
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);

Expand Down Expand Up @@ -2540,6 +2540,52 @@
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', () => {
Expand Down