How do I catch an error that gets thrown asynchronously by Reflect.get(target, name, receiver) within the Proxy handler?
I'm using Proxy and Reflect to wrap an API class, so that I can catch any Network exceptions and gracefully display error messages to the user.
//Wrapper.js
let handler = {
get: (target, name, receiver) => {
try {
Reflect.get(target, name, receiver); //throws Error asynchronously
} catch (e) {
console.log('caught error', e);
}
};
export default new Proxy(new API(), handler);
//App.js import Wrapper from './Wrapper'; Wrapper.asyncFunction(); //throws uncaught Error
//API.js
class API {
get user() {
return new User()
}
}
//User.js
class User {
/**
* List all users
*/
all() {
return new Promise((resolve, reject) => {
reject(new Error('Network error'));
});
}
}
When an error is thrown, inside Reflect.get() "caught error" never gets printed and remains uncaught.