Replies: 3 comments 5 replies
-
@BanksySan, I'm struggling to fully understand the issue you are experiencing based on the PoC you shared. Could you please simplify that example a bit and maybe add some comments explaining what's expected and the actual result you're seeing? Maybe extracting the example from Checkout would be even better 😄 |
Beta Was this translation helpful? Give feedback.
-
Isn't this only an issue if the code inside the Promise isn't wrapped in a try-catch and rejecting in the catch? You would expect axios to be rejecting its Promises. An exception thrown from directly within an async method is caught, so long as the call is awaited. If we do this: async function throwAfterAwait(){
await new Promise(r => setTimeout(r, 100));
throw new Error("Thrown after await");
}
async function call(){
try {
await throwAfterAwait();
} catch (error) {
console.log('caught');
}
}
call(); we get I've just compared this to your example and it's the |
Beta Was this translation helpful? Give feedback.
-
You have a point there. It seems that the async function throwImmediate() {
console.log('Call throwImmediate');
throw new Error("Error throwImmediate thrown");
}
(async () => {
try {
await throwImmediate();
} catch (e) {
console.log(`throwImmediate Caught: "${e.message}"`);
} finally {
console.log('throwImmediate Finally');
}
console.log('throwImmediate Finished');
})();
async function throwInPromise() {
console.log('Call throwInPromise');
return new Promise((_req, _rej) => {
throw new Error("Error throwInPromise thrown");
});
}
(async () => {
try {
await throwInPromise();
} catch (e) {
console.log(`throwInPromise Caught: "${e.message}"`);
} finally {
console.log('throwInPromise Finally');
}
console.log('throwInPromise Finished');
})();
However, when the error is thrown in async function throwInPromiseTimeout() {
return new Promise((_, ___) => {
setTimeout(() => {
throw new Error("Throw throwInPromiseTimeout error");
}, 1000);
});
}
(async () => {
try {
await throwInPromiseTimeout();
} catch (e) {
console.log(`throwInPromiseTimeout Caught: "${e.message}"`);
} finally {
console.log('throwInPromiseTimeout Finally');
}
console.log('throwInPromiseTimeout Finished');
})();
|
Beta Was this translation helpful? Give feedback.
-
The Problem
When an error is thrown in from a promise or an async function we can’t use
try-catch-finally
to handle catch and handle it. The JavaScript engine has already passed/disposed of the catching block when the error bubbles up.Currently we have a lot of
try-catch-finally
blocks failing to catch errors in async methods. The main example of this is errors thrown by Axios when an HTTP call fails. This causes null reference exceptions and promise not finished exceptions to be thrown further down the line.For Discussion
All errors must be handled so that only system-level errors bubble (i.e. out of memory, a volcano has erupted, the cleaner unplugged the server).
PR Reviewers need to know about this in order to look for it when reviewing.
We need to agree on a standard pattern(s) that we can advise PR authors use in place of
try-catch-finally
.We need to review the existing code and re-write parts that are incorrectly using
try-catch-finally
.Examples:
axios
in f-checkout'sCheckout.vue
wrapped intry-catch-finally
.Beta Was this translation helpful? Give feedback.
All reactions