Application: error event
The error event fires when there is an uncaught exception. The runtime dispatches it itself; it doesn't come from the system.
Syntax
application.addEventListener('error', (event) => { })Event object
| Field | Type | Meaning |
|---|---|---|
message | string | A summary of the error, generated by the engine |
error | unknown | The value that was thrown |
filename | string | The name of the script where the error occurred, such as 'main.js' |
lineno | number | The line number where the error occurred |
target is application. cancelable is true: calling preventDefault() stops the runtime from printing the default error log.
When it fires
- When an exception occurs at the top level of
main.js, in a timer callback, or in an event listener callback (including thesceneconnectcallback and button tap callbacks) and isn't caught bytry/catch. The script top level is verified; timer callbacks are not verified in the simulator. - When an event listener throws,
errorfires before the next listener for the same event runs, and the remaining listeners run as usual (verified by testing). - An exception thrown by an
errorlistener itself doesn't fireerroragain; only the default log is printed to the console. Promiserejections don't go here; they go tounhandledrejection.- When no listener calls
preventDefault(), the runtime prints a log starting withUncaughtat theconsole.errorlevel. Calling it cancels only this log: the exception has already happened, and the interrupted code doesn't resume.
Examples
js
application.addEventListener('error', (e) => {
console.log('Error location', e.filename, e.lineno);
console.log('Is an Error object', e.error instanceof Error);
e.preventDefault();
});
throw new Error('Nobody catches this');Result
- The console prints
Error location main.jsfollowed by the line number of thethrowline. - The console prints
Is an Error object true. - Because
preventDefault()was called, the runtime doesn't print the default error log.
Note: Put the listener at the very top of
main.js. It doesn't receive exceptions thrown by top-level code that comes before it.
Corresponding iOS API
There's no corresponding system API. Corresponds to the browser's error event on window.