Description
We currently expose the [uncaughtException](
https://docs.appcelerator.com/platform/latest/#!/api/Titanium.App-event-uncaughtException) event on
Titanium.App
to handle errors that get thrown but not handled. However this does not include unhandled promise rejections or errors from within async functions
As we include core-js there is a default handler for unhandled error in promises that can also be overriden by a user like below, but this means that there is no integration with something like ACA, or the error dialog.
global.onunhandledrejection = e => console.log('unhandled', e.reason, e.promise);
From testing also, we are not consistent across platforms. On iOS an async function will be caught by
onunhandledrejection
as it is transpiled down to promises, but on android the error will be swallowed
Below is code that throws 3 types of errors
const win = Ti.UI.createWindow()
const lbl = Ti.UI.createLabel({ text: 'Callback error', top: 100 });
lbl.addEventListener('click', () => {
console.log('throwing from callback');
throw new Error('Callback error');
});
const lbl2 = Ti.UI.createLabel({ text: 'Promise thrown', top: 250 });
lbl2.addEventListener('click', () => {
Promise.resolve()
.then(() => {
console.log('throwing from promise');
throw new Error('Promise thrown error')
})
});
const lbl3 = Ti.UI.createLabel({ text: 'Async thrown', top: 400 });
lbl3.addEventListener('click', async () => {
console.log('throwing from async');
throw new Error('Async function thrown error');
});
Ti.App.addEventListener('uncaughtException', (e) => {
console.log('uncaughtException handler');
console.log(e);
});
win.add(lbl);
win.add(lbl2);
win.add(lbl3);
win.open();
I'm unsure on the expected result here, it might make sense for us to ensure that errors from async functions/promises are handled via the uncaughtException event
No comments