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

Fix handling of numeric error codes coming from AWS SDK requests #9538

Merged
merged 3 commits into from
Jun 2, 2021
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
6 changes: 5 additions & 1 deletion lib/aws/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ async function awsRequest(service, method, ...args) {
providerError: Object.assign({}, err, { retryable: false }),
});
}
const providerErrorCodeExtension = err.code ? normalizeErrorCodePostfix(err.code) : 'ERROR';
const providerErrorCodeExtension = (() => {
if (!err.code) return 'ERROR';
if (typeof err.code === 'number') return `HTTP_${err.code}_ERROR`;
return normalizeErrorCodePostfix(err.code);
})();
throw Object.assign(
new ServerlessError(
process.env.SLS_DEBUG && err.stack ? `${err.stack}\n${'-'.repeat(100)}` : message,
Expand Down
34 changes: 31 additions & 3 deletions test/unit/lib/aws/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,38 @@ describe('#request', () => {
return expect(sendFake.promise).to.have.been.calledOnce;
});

it('should expose non-retryable errors', () => {
it('should expose non-retryable errors', async () => {
const error = {
statusCode: 500,
message: 'Some error message',
code: 'SomeError',
};
class FakeS3 {
error() {
test() {
return {
promise: async () => {
throw error;
},
};
}
}
const awsRequest = proxyquire('../../../../lib/aws/request', {
'aws-sdk': { S3: FakeS3 },
});
await expect(awsRequest({ name: 'S3' }, 'test')).to.eventually.be.rejected.and.have.property(
'code',
'AWS_S3_TEST_SOME_ERROR'
);
});

it('should handle numeric error codes', async () => {
const error = {
statusCode: 500,
message: 'Some error message',
code: 500,
};
class FakeS3 {
test() {
return {
promise: async () => {
throw error;
Expand All @@ -246,7 +271,10 @@ describe('#request', () => {
const awsRequest = proxyquire('../../../../lib/aws/request', {
'aws-sdk': { S3: FakeS3 },
});
return expect(awsRequest({ name: 'S3' }, 'error')).to.be.rejected;
await expect(awsRequest({ name: 'S3' }, 'test')).to.eventually.be.rejected.and.have.property(
'code',
'AWS_S3_TEST_HTTP_500_ERROR'
);
});
});

Expand Down