# Should I throw exceptions in a Trigger?

**URL:** <https://community.auth0.com/t/should-i-throw-exceptions-in-a-trigger/170286>\
**Category:** Get Help\
**Tags:** quickstarts-backend-api, sdks-quickstarts\
**Created:** [January 25, 2025, 4:12pm UTC](https://community.auth0.com/t/should-i-throw-exceptions-in-a-trigger/170286 "2025-01-25T16:12:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VictorioBerra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/victorioberra/32/28773_2.png) [@VictorioBerra](https://community.auth0.com/u/VictorioBerra)\
**Post date:** [January 25, 2025, 4:12pm UTC](https://community.auth0.com/t/should-i-throw-exceptions-in-a-trigger/170286/1 "2025-01-25T16:12:25Z")

</div>

Which of the below is the acceptable way to do what I am trying to accomplish?

```auto
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');
    }

};

```

---

<div class="post-metadata">

**Author:** ![sumansaurav](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sumansaurav/32/70627_2.png) [@sumansaurav](https://community.auth0.com/u/sumansaurav)\
**Post date:** [January 25, 2025, 7:27pm UTC](https://community.auth0.com/t/should-i-throw-exceptions-in-a-trigger/170286/2 "2025-01-25T19:27:28Z")

</div>

The first approach using `api.access.deny()` is more appropriate here because:

1. It uses the provided API mechanism to properly handle authentication failures
2. It logs the error for debugging
3. It explicitly denies access through the authentication system
4. It maintains better control flow

The `throw new Error()` approach would:

1. Crash the execution without proper error handling
2. Not leverage the authentication API’s built-in functionality
3. Miss logging the failure

---

<div class="post-metadata">

**Author:** ![VictorioBerra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/victorioberra/32/28773_2.png) [@VictorioBerra](https://community.auth0.com/u/VictorioBerra)\
**Post date:** [January 26, 2025, 9:41pm UTC](https://community.auth0.com/t/should-i-throw-exceptions-in-a-trigger/170286/3 "2025-01-26T21:41:25Z")

</div>

> 1. It maintains better control flow

[I have another thread asking about this that no one replied to yet. Maybe you can answer it.](https://community.auth0.com/t/deny-plus-redirect-in-action-triggers/170287)

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).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 26, 2025, 5:10pm UTC](https://community.auth0.com/t/should-i-throw-exceptions-in-a-trigger/170286/4 "2025-02-26T17:10:22Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
