Skip to content

Commit

Permalink
Merge fd206d6 into 773f37a
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmarzex committed Sep 19, 2023
2 parents 773f37a + fd206d6 commit b96d2be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
11 changes: 8 additions & 3 deletions api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = async (pathname, options = {}) => {
}
})();
log.debug('[%d] %d, %o', requestId, response.status, response.headers);
const slsCorrelationId = response.headers.get('sls-correlation-id');
if (!response.ok) {
const responseText = await response.text();
log.debug('[%d] %s', requestId, responseText);
Expand All @@ -64,15 +65,17 @@ module.exports = async (pathname, options = {}) => {
);
}
throw Object.assign(
new Error(`Dashboard server error: [${response.status}] ${responseText}`),
new Error(
`Dashboard server error: [${response.status}] ${responseText}. ReferenceId: ${slsCorrelationId}`
),
{
code: `DASHBOARD_SERVER_ERROR_${response.status}`,
httpStatusCode: response.status,
}
);
}
throw new ServerlessError(
'Dashboard server is unavailable, please try again later',
`Dashboard encountered an error, please try again later. ReferenceId: ${slsCorrelationId}`,
'DASHBOARD_SERVER_REQUEST_FAILED'
);
}
Expand All @@ -83,7 +86,9 @@ module.exports = async (pathname, options = {}) => {
} catch (error) {
const responseText = await response.text();
log.debug('[%d] %s', requestId, responseText);
throw new Error(`Dashboard server error: received unexpected response: ${responseText}`);
throw new Error(
`Dashboard server error: ${responseText}. ReferenceId: ${slsCorrelationId}`
);
}
})();
log.debug('[%d] %o', requestId, responseData);
Expand Down
32 changes: 28 additions & 4 deletions test/api-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ describe('test/api-request.test.js', () => {
if (url.includes('/server-error/')) {
return {
status: 500,
headers: responseHeaders,
headers: new Map(
Object.entries({
...responseHeaders,
'sls-correlation-id': 'correlation-id-123',
})
),
text: async () => 'Server Error',
};
}
if (url.includes('/programmer-error/')) {
return {
status: 400,
headers: responseHeaders,
headers: new Map(
Object.entries({
...responseHeaders,
'sls-correlation-id': 'correlation-id-123',
})
),
text: async () => 'Programmer Error',
};
}
Expand Down Expand Up @@ -144,12 +154,26 @@ describe('test/api-request.test.js', () => {
expect(
api('/server-error/', { accessKey: testAccessKey })
).to.eventually.be.rejected.and.have.property('code', 'DASHBOARD_SERVER_REQUEST_FAILED'));

it('should handle server error and include correlationId', async () => {
expect(
api('/server-error/', { accessKey: testAccessKey })
).to.eventually.be.rejected.and.have.property(
'message',
'Dashboard encountered an error, please try again later. ReferenceId: correlation-id-123'
);
});
it('should handle programmer error', async () =>
expect(
api('/programmer-error/', { accessKey: testAccessKey })
).to.eventually.be.rejected.and.have.property('code', 'DASHBOARD_SERVER_ERROR_400'));

it('should handle progammer error and include correlationId', async () => {
expect(
api('/programmer-error/', { accessKey: testAccessKey })
).to.eventually.be.rejected.and.have.property(
'message',
'Dashboard server error: [400] Programmer Error. ReferenceId: correlation-id-123'
);
});
it('should handle user auth error', async () =>
expect(
api('/user-auth-error/', { accessKey: testAccessKey })
Expand Down

0 comments on commit b96d2be

Please sign in to comment.