Which of the below is the acceptable way to do what I am trying to accomplish?
exports.onExecutePostLogin = async (event, api) => {
const ApiKey = event.secrets.CIVICRM_API_KEY;
// Should I do this?
if (!ApiKey) {
console.log( `APIKEY is not configured` );
api.access.deny('APIKEY is not configured');
return;
}
// Or this?
if (!ApiKey) {
throw new Error('APIKEY is not configured');
}
};
The first approach using api.access.deny()
is more appropriate here because:
- It uses the provided API mechanism to properly handle authentication failures
- It logs the error for debugging
- It explicitly denies access through the authentication system
- It maintains better control flow
The throw new Error()
approach would:
- Crash the execution without proper error handling
- Not leverage the authentication API’s built-in functionality
- Miss logging the failure
- It maintains better control flow
I have another thread asking about this that no one replied to yet. Maybe you can answer it.
Essentially, can I call api.access.deny()
anywhere in my code and essentially process.exit(1)
happens? Because if I throw an Exception we come to a full stop and I dont have potentially undefined things being passed around (which ultimately leads to a less discoverable exception thrown).