Skip to content

Commit

Permalink
fix: Fix handling of numeric error codes coming from AWS SDK requests
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jun 1, 2021
1 parent 2ab3234 commit b740a08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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
24 changes: 24 additions & 0 deletions test/unit/lib/aws/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ describe('#request', () => {
'AWS_S3_TEST_SOME_ERROR'
);
});

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

it('should expose original error message in thrown error message', () => {
Expand Down

0 comments on commit b740a08

Please sign in to comment.