# Crash caused by timeout -\> where to look?

**URL:** <https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244>\
**Category:** Developers\
**Tags:** app, homey-pro\
**Created:** [September 21, 2022, 5:17pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244 "2022-09-21T17:17:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 21, 2022, 5:17pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/1 "2022-09-21T17:17:03Z")

</div>

I get a lot of the following crash reports from my app, but I can’t figure out what the cause is. Does anyone have an idea where to look for the error?

```auto
Note: this e-mail is sent because an unhandledRejection occurred in your app. Make sure to catch all errors, this might crash your app in the future. Stack trace attached below:

  Error: Timeout after 5000ms
    at Function.timeout (/node_modules/homey-api/lib/Util.js:57:26)
    at HomeyAPIApp.call (/node_modules/homey-api/lib/HomeyAPI/HomeyAPIV2.js:359:28)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ManagerDevices.value (/node_modules/homey-api/lib/HomeyAPI/HomeyAPIV2/Manager.js:261:24)

```

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 21, 2022, 5:28pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/2 "2022-09-21T17:28:51Z")

</div>

Are you using [App API calls](https://apps.developer.homey.app/advanced/web-api)?

---

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 21, 2022, 5:42pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/3 "2022-09-21T17:42:24Z")

</div>

Yes, I both use api-calls through

> this.homey.api.getApiApp(‘xxxxx’)

and

> new HomeyAPIApp({  
> homey: this.homey  
> });

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 21, 2022, 5:52pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/4 "2022-09-21T17:52:01Z")

</div>

It looks like the issue is caused by that, the other app apparently doesn’t respond quickly enough.

---

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 23, 2022, 11:32am UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/5 "2022-09-23T11:32:48Z")

</div>

Yeah, that sounds logical, but the problem is that I for testing replaced every single API call with a throw(new Error(“test error”)) and it turned out that all of these were caught by my crash protection code… Thus I still have no idea why there is an uncaught crash within these API calls ???

---

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 23, 2022, 11:57am UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/6 "2022-09-23T11:57:09Z")

</div>

I see from the code that the crash is reported from

```auto
/node_modules/homey-api/lib/HomeyAPI/HomeyAPIV2/Manager.js:261:24

```

```auto
248: if (this.homey.isConnected() && $socket === true) {
249: result = await Util.timeout(new Promise((resolve, reject) => {
250: this.homey.__ioNamespace.emit('api', {
251: args,
252: operation: operationId,
253: uri: this.uri,
254: }, (error, result) => {
255: if (error) return reject(new HomeyAPIError({ error }));
256: return resolve(result);
257: });
258: }), $timeout);
259: } else {
260: // Get from HTTP
261: result = await this.homey.call({
262: $timeout,
263: headers,
264: body,
265: path: `/api/manager/${this.id}${path}`,
266: method: operation.method,
267: });
268: }

```

It appears from line 249 that a Promise should have been rejected and returned in the code for line 261, but instead it doesn’t seem to be any exception handling here… So I guess this is a problem with the Athom node module code.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 23, 2022, 12:09pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/7 "2022-09-23T12:09:55Z")

</div>

> [@frodeheg](#):
>
> but instead it doesn’t seem to be any exception handling here… So I guess this is a problem with the Athom node module code.

Or it assumes that any exceptions are handled higher up, like in your code.

---

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 23, 2022, 12:45pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/8 "2022-09-23T12:45:05Z")

</div>

> [@robertklep](#):
>
> Or it assumes that any exceptions are handled higher up, like in your code.

But then, why is it not sent to my code? I mean, I would have seen a crash myself when I replaced all api-calls with throw(new Error(“xxx”)), but I didn’t because everything was caught by my exception handlers…

---

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 23, 2022, 12:51pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/9 "2022-09-23T12:51:15Z")

</div>

As it is a timeout I assume Homey is heavily loaded. Could it be that a function in my own code is called multiple times without having finished its last run and as such overwrite some state and then Homey loses track of the calling function?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 23, 2022, 1:21pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/10 "2022-09-23T13:21:15Z")

</div>

Losing track (or rather: context) could happen if callbacks are mixed with promises.

---

<div class="post-metadata">

**Author:** ![frodeheg](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/frodeheg/32/49010_2.png) [@frodeheg](https://community.homey.app/u/frodeheg)\
**Post date:** [September 23, 2022, 8:24pm UTC](https://community.homey.app/t/crash-caused-by-timeout-where-to-look/69244/11 "2022-09-23T20:24:28Z")

</div>

This error was really hard to find but I think I nailed it now.

So I thought I was smart to do some “prefetching” as such:

```auto

promiseDevice = this.homeyApi.devices.getDevice({ id: deviceId });
....lots of code....
try {
    let device = await promiseDevice;
} catch(err) {
    ...
}

```

but apparently, I had a conditional `return` statement within the `...lots of code...` block.  
Not so sure it was so smart after all 😝
