Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Serverless Platform Correlation Id in error messages #222

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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