Skip to content

Commit eca5826

Browse files
Handle errors with circular references in done (#41)
* handle errors with circular references in done * fix linting errors
1 parent 88dd581 commit eca5826

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

al_aws_collector.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
'use strict';
1212

13+
const util = require('util');
1314
const AWS = require('aws-sdk');
1415
const moment = require('moment');
1516
const zlib = require('zlib');
@@ -119,7 +120,26 @@ class AlAwsCollector {
119120
done(error) {
120121
let context = this._invokeContext;
121122
if (error) {
122-
return context.fail(error);
123+
// The lambda context tires to stringify errors, so we should check if they can be stringified before we pass them to the context
124+
try{
125+
const stringifiedError = JSON.stringify(error);
126+
return context.fail(error);
127+
}
128+
catch (stringifyError){
129+
// Can't stringify the whole error, so lets try and get some useful info from it
130+
let errorString;
131+
132+
if (error.toJSON){
133+
errorString = error.toJSON();
134+
} else if (error.message){
135+
errorString = error.message;
136+
} else {
137+
// when all else fails, stringify it the gross way with inspect
138+
errorString = util.inspect(error);
139+
}
140+
141+
return context.fail(errorString);
142+
}
123143
} else {
124144
return context.succeed();
125145
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alertlogic/al-aws-collector-js",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"license": "MIT",
55
"description": "Alert Logic AWS Collector Common Library",
66
"repository": {

test/al_aws_collector_test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,71 @@ describe('al_aws_collector tests', function() {
603603
});
604604
});
605605

606+
describe('done() function', () => {
607+
var collector;
608+
609+
it('calls success when there is no error', () => {
610+
611+
const testContext = {
612+
succeed: () => true,
613+
fail: () => false
614+
};
615+
616+
collector = new AlAwsCollector(
617+
testContext,
618+
'cwe',
619+
AlAwsCollector.IngestTypes.SECMSGS,
620+
'1.0.0',
621+
colMock.AIMS_TEST_CREDS
622+
);
623+
624+
const doneResult = collector.done();
625+
assert.ok(doneResult);
626+
});
627+
628+
it('returns errors that can be stringified in their raw state', () => {
629+
const stringifialbleError = {
630+
foo: "bar"
631+
};
632+
const testContext = {
633+
succeed: () => true,
634+
fail: (error) => error
635+
};
636+
637+
collector = new AlAwsCollector(
638+
testContext,
639+
'cwe',
640+
AlAwsCollector.IngestTypes.SECMSGS,
641+
'1.0.0',
642+
colMock.AIMS_TEST_CREDS
643+
);
644+
645+
const doneResult = collector.done(stringifialbleError);
646+
assert.ok(doneResult === stringifialbleError);
647+
});
648+
649+
it('returns errors that cannot be JSON stringified as a string', () => {
650+
const circRefError = {};
651+
circRefError.foo = circRefError;
652+
const testContext = {
653+
succeed: () => true,
654+
fail: (error) => error
655+
};
656+
657+
collector = new AlAwsCollector(
658+
testContext,
659+
'cwe',
660+
AlAwsCollector.IngestTypes.SECMSGS,
661+
'1.0.0',
662+
colMock.AIMS_TEST_CREDS
663+
);
664+
665+
const doneResult = collector.done(circRefError);
666+
assert.ok(doneResult !== circRefError);
667+
assert.ok(typeof doneResult === 'string');
668+
});
669+
});
670+
606671
describe('applyConfigChanges() function', () => {
607672
var object;
608673
var newValues;

0 commit comments

Comments
 (0)