ioBroker.javascript: setStateAsync with unknown id and no try/catch crashes javascript adatper
Describe the bug
setStateAsync with unknown id and no try/catch crashes javascript adatper
To Reproduce
Steps to reproduce the behavior:
- Write Javascript-Script with following code:
aync function xy() { await setStateAsync('alias.0.somethingnotexisting', true); }; xy();
(maybe the await is not necessary, but I had it in my code) - Save the script.
- Check log files and see that javascript adapter crashes with unhandle promise rejection
Expected behavior
Javascript Adapter should try to catch this error and write an error to logfile and not crash with stopping all scripts. 😃
Screenshots & Logfiles
2021-02-14 11:25:02.775 - info: javascript.0 (614739) Start javascript script.js.otherSmarthome.fusswaermerAutoAn
2021-02-14 11:25:02.782 - info: javascript.0 (614739) script.js.otherSmarthome.fusswaermerAutoAn: registered 1 subscription and 0 schedules
2021-02-14 11:25:02.783 - info: javascript.0 (614739) script.js.otherSmarthome.fusswaermerAutoAn: Schalte Fusswärmer an wegen Stromverbrauch von 81.442W.
2021-02-14 11:25:02.783 - warn: javascript.0 (614739) State "alias.0.plugs.Fusswarmer.state" not found
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at setState (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1417:20)
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at /opt/iobroker/node_modules/iobroker.javascript/lib/tools.js:80:16
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at new Promise (<anonymous>)
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at /opt/iobroker/node_modules/iobroker.javascript/lib/tools.js:79:16
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at switchOnOff (script.js.otherSmarthome.fusswaermerAutoAn:15:11)
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at runMicrotasks (<anonymous>)
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at runNextTicks (internal/process/task_queues.js:62:5)
2021-02-14 11:25:02.784 - warn: javascript.0 (614739) at processImmediate (internal/timers.js:434:9)
2021-02-14 11:25:02.785 - error: javascript.0 (614739) Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(
2021-02-14 11:25:02.785 - error: javascript.0 (614739) unhandled promise rejection: undefined
2021-02-14 11:25:02.785 - error: javascript.0 (614739) undefined
2021-02-14 11:25:02.821 - info: javascript.0 (614739) Stop script script.js.multimedia.toonieBoxClon
2021-02-14 11:25:02.821 - info: javascript.0 (614739) Stop script script.js.Licht.bueroMehrLicht
2021-02-14 11:25:02.821 - info: javascript.0 (614739) Stop script script.js.otherSmarthome.GasCounter
2021-02-14 11:25:02.822 - info: javascript.0 (614739) Stop script script.js.multimedia.repairAudio
2021-02-14 11:25:02.822 - info: javascript.0 (614739) Stop script script.js.Telegram.sendToUser
....
2021-02-14 11:25:02.832 - info: javascript.0 (614739) Stop script script.js.KNX.reactToButtons
2021-02-14 11:25:02.832 - info: javascript.0 (614739) Stop script script.js.Schlafen.kinderSchlafen
2021-02-14 11:25:02.832 - info: javascript.0 (614739) Stop script script.js.otherSmarthome.fusswaermerAutoAn
2021-02-14 11:25:02.835 - info: javascript.0 (614739) terminating
2021-02-14 11:25:02.835 - warn: javascript.0 (614739) Terminated (UNCAUGHT_EXCEPTION): Without reason
2021-02-14 11:25:03.628 - error: host.Server Caught by controller[0]: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise
2021-02-14 11:25:03.630 - error: host.Server Caught by controller[1]: State "alias.0.plugs.Fusswarmer.state" not found
2021-02-14 11:25:03.630 - error: host.Server instance system.adapter.javascript.0 terminated with code 6 (UNCAUGHT_EXCEPTION)
It seems to stop all scripts before exit. But still it exits.
Versions:
- Adapter version: 4.10.15
- JS-Controller version: 3.2.16
- Node version: v12.20.1
- Operating system: Ubuntu 20.04 (or so) on x86_64 hardware.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 19 (14 by maintainers)
Commits related to this issue
- make promisified methods reject with Error instead of string fixes #757 — committed to Garfonso/ioBroker.javascript by Garfonso 3 years ago
I tried to understand your proposed solution and implemented it here: https://github.com/Garfonso/ioBroker.javascript/commit/83ff6407f2bc8a30efdef93a47066767b50dab59
With that change the rejection is handled and an error message printed and the adapter keeps running. If the change is what you meant, I’ll open a PR.