# Hook - Code generated an uncaught exception: Error: Callback was already called

**URL:** <https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722>\
**Category:** Get Help\
**Tags:** hooks, callback\
**Created:** [November 13, 2017, 5:31pm UTC](https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722 "2017-11-13T17:31:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xie.williams](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@xie.williams](https://community.auth0.com/u/xie.williams)\
**Post date:** [November 13, 2017, 5:31pm UTC](https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722/1 "2017-11-13T17:31:26Z")

</div>

I’m using a Client Credentials Exchange hook, and while it’s functioning correctly I’m seeing this error in the logs:

```
Code generated an uncaught exception: Error: Callback was already called.

```

Here’s the hook, note there’s only one invocation of `cb`.

```
module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;

  if (context && context.body && context.body'foo']) {
    access_token'https://prefix.com/foo'] = context.body'foo'];
  }

  cb(null, access_token);
};

```

Are there circumstances under which the same hook may be called twice with the same callback function, and I need to guard against that? Additionally we are also using rules - is the same callback passed to each?

Full stacktrace:

```
Code generated an uncaught exception: Error: Callback was already called.
  at /data/sandbox/node_modules/async/dist/async.js:903:32
  at /data/sandbox/node_modules/async/dist/async.js:3858:13
  at /data/sandbox/lib/sandbox.js:800:36
  at ensure_request_consumed (/data/sandbox/lib/sandbox.js:333:36)
  at /data/sandbox/lib/sandbox.js:794:32
  at /data/sandbox/node_modules/auth0-authz-rules-api/index.js:49:9
  at /data/io/7298df97c3a64268970ccbc79deaac44/webtask.js:178:38
  at organizationPicker (/data/io/7298df97c3a64268970ccbc79deaac44/webtask.js:176:3)
  at fn (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/async/lib/async.js:638:34)
  at Immediate._onImmediate (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/async/lib/async.js:554:34)
  at processImmediate [as _immediateCallback] (timers.js:396:17)

```

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [November 13, 2017, 7:11pm UTC](https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722/2 "2017-11-13T19:11:58Z")

</div>

Based on the stack-trace provided the error is coming from one of the rules and not from your client credentials hook (which is correctly invoking the callback only once). You should review your rules in order to ensure none is calling the callback function more than once; the most common issue is with rules that perform asynchronous operations and for which you do not remove the default callback call performed as the last line of the rule template.

---

<div class="post-metadata">

**Author:** ![xie.williams](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@xie.williams](https://community.auth0.com/u/xie.williams)\
**Post date:** [November 13, 2017, 10:40pm UTC](https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722/3 "2017-11-13T22:40:53Z")

</div>

Haha, of course - it’s the same log stream. Cheers mate, I’ll look into the rules then. And thanks for the quick response.

---

<div class="post-metadata">

**Author:** ![xie.williams](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@xie.williams](https://community.auth0.com/u/xie.williams)\
**Post date:** [November 13, 2017, 11:16pm UTC](https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722/4 "2017-11-13T23:16:22Z")

</div>

You called it. Sure enough one of our rules does exactly as you say. Cheers again.

---

<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:** [March 2, 2018, 5:57am UTC](https://community.auth0.com/t/hook-code-generated-an-uncaught-exception-error-callback-was-already-called/8722/5 "2018-03-02T05:57:22Z")

</div>


