feathers-vuex
feathers-vuex copied to clipboard
clearError mutation needs to be used manually
The Service Plugin documentation for Mutations for Managing Errors states:
The following mutations are called automatically by the service actions, and will rarely need to be used manually.
While this is true for setError, it does not seem to be the case for clearError. I could not find any usage of this mutation in the code base and also an error remains set, even if the request could be made successfully the second time.
While this feels like a documentation issue, I'm also sympathetic to the idea of automatically calling clearError for the appropriate method before sending a request.
For sure the documentation needs to be updated.
As for automatically calling clearError, it's a little bit more complicated since each service is shared. An example is when you have multiple components on the page which all use the same service. Each component is independently querying and only one of them is likely to fail. It's currently up to the developer to preserve that error message if it's required. Auto-clearing the error would make it so the message doesn't persist while other requests are happening, which would likely make it more confusing for a new dev.
I agree, clearing this (per service) global error state automatically would not be a good idea.
As I'm using FeathersVuexFormWrapper and have to catch the Promise rejection of .save() anyway, I went with the following solution, which ties the error handling directly to the request:
<FeathersVuexFormWrapper :item="item" watch>
<template v-slot="{ clone, save, reset }">
<UserEditor
:item="clone"
@save="
() => {
$data.formError = null
save()
.then(() => $router.push({name: 'user-list'}))
.catch(reason => { $data.formError = reason })
}"
@reset="reset"
></UserEditor>
</template>
</FeathersVuexFormWrapper>
Thanks @abrain! This will be a good example for the docs, so I'll leave this open until its added by one of us.