I am trying to properly structure my AWS lambda. I am attempting to exit the Lambda when I encounter an error. With the simple one function Lambda, I do this:
exports.handler = async (event, context) => {
const someError = new Error('Something is wrong');
throw someError;
};
It ends up in DeadQueue as expected (after several tries).
When I want to resolve it with success, I do this:
exports.handler = async (event, context) => {
// ... some code here
return {};
};
Now I want to structure my application using requires, so I have something like this:
//Main execution point
const validator = require('./Validate/Validator');
exports.handler = async (event, context) => {
let aaa = validator.validate(operationName);
console.log('I should not bere here')
};
And the validator itself:
exports.validate = async (schemaName, payload) => {
console.log('I am coming here')
try {
const Schema = require(`../ValidationSchemas/${schemaName}`).schema;
}
catch (e) {
const schemaError = new Error('Validation schema not found. Operation does not exist.');
throw schemaError;
//process.exit(0);
}
};
What happens in validator, if I throw an error, error is thrown, but my execution is continued in the main (caller) lambda function. I thought to stop it there using
process.exit(0)
It does work. Lambda is terminated. But it looks like a bad approach for some reason. Ideally I would do it from the main function, but I am thinking about the best approach.