-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to trigger error messages without the template? #76
Comments
Ok I figured out I can chain a <script setup lang="ts">
import { useTask, timeout } from 'vue-concurrency';
import { useQuasar } from 'quasar';
const $q = useQuasar();
const triggerNotification = (errorMessage: string) => {
$q.notify(errorMessage);
};
const throwError = async () => {
await timeout(1);
throw new Error('Ruh oh! Error.');
};
const exampleTask = useTask(function* () {
yield throwError().catch((err) => {
triggerNotification(err.message);
});
});
</script>
<template>
<q-btn label="Run Task" @click="exampleTask.perform"></q-btn>
</template> With this example it seems simple, but in real life I have many Tasks and each contains many yield statements within. Do I have to chain a That looks messy and verbose. I thought I could implement a Maybe there is another solution where I can group together all the tasks and apply one |
hi @BenJackGill
I think this depends on how you want to handle the errors. I sometimes use these popup notifications as well - they're good for cases when there's no relevant place to display the error, and especially if something breaks in the background. So my workflow usally is 90% times - display error "in place" and use Buf if you plan to use What you can do is wrap the entire task perform in a try catch. <script setup lang="ts">
import { useTask, timeout } from 'vue-concurrency';
import { useQuasar } from 'quasar';
const $q = useQuasar();
const throwError = async () => {
await timeout(1);
throw new Error('Ruh oh! Error.');
};
const exampleTask = useTask(function* () {
yield throwError().catch((err) => {
triggerNotification(err.message);
});
});
const save = async () => {
try {
await exampleTask.perform();
} catch (e) {
$q.notify(error.message);
}
};
</script>
<template>
<q-btn label="Run Task" @click="save"></q-btn>
</template> You'd be creating these wrapper functions a lot, so you can write some higher order function for it: const performAndNotify = async (task, ...args) => {
try {
await task.perform(...args);
} catch (error) {
$q.notify(error.message);
}
}; Then you can reuse this function at multiple places. |
Sorry for my late reply, I was hit with Covid, but am better now. Thanks for such a thoughtful answer. It was very helpful. I think I will do as you say, and keep 90% of error messages in place instead of putting them all into a popup notification. |
Great!:) |
Would you be opposed to including event handlers as optional parameters? For example, here in TaskInstance.ts line 217:
If that also called any optionally attached event handlers then outside functions could hook in and request updates upon errors. Additionally, could define a global event handler for If you're not opposed to the above I might spend some time implementing it and submitting a PR for it. |
I think passing a callback like const myAction = () => {
try {
task.perform()
} catch (e) {
}
} It's a bit more verbose but it enforces better logic flow I think. But the global I also proposed it here: Just from the top of my mind I'm not sure how to implement that. That functions has to be store somewhere. A lot of other plugins use the vue app instance for that that would require additional setup of PR or any ideas for this welcome! |
It will likely be two PRs then, one for the global and another for the local. I can see advantages to the local scoped event handler and even some use cases for it but I understand the issues that might arise from that pattern. |
ok, sounds good 👍 for the local one it would be good to add a disclaimer to the docs not to overuse it |
I am trying to use Vue Concurrency with my Vue 3 / TypeScript / Quasar app.
Previously, I was handling the display of Task error messages in the template with a
v-if
like this:But now I am using Quasar Notify to display error messages in a popup. These popups are not written into the template. Instead they are triggered from the script, like this:
But I don't know how to connect the
error
generated by the Task to thetriggerNotification
function.To give a final overview, this is where the code currently stands, without knowing how to correctly connect the
error
generated by the Task to thetriggerNotification
function:Do you have any guidance on how to get this working?
The text was updated successfully, but these errors were encountered: