next
next copied to clipboard
[Core] Error handling
- Composables return
errors
object that contains all of errors under the keys that are defined in the core. - A developer can use composable function and the
errors
field in the theme and is able to react on particular error itself.
example of composable function:
const useUser = () => {
connst errors = ref({});
const registerUser = () => {
try {
// ..
} catch (e) {
errors.register = e;
}
};
const loginUser = () => {
try {
// ..
} catch (e) {
errors.login = e;
}
}
return {
errors,
registerUser,
loginUser
};
}
now in the theme you can react on each error type:
<template>
<div>
<div v-if="errors.register">There was a problem with registering: {{ errors.register }}</div>
<div v-if="errors.login">There was a problem with login: {{errors.login }}</div>
</div>
</template>
setup() {
const { errors, ... } = useUser()
// ...
return { errors };
}
I believe const errors = ref({});
should be already defined with properties which it contains
connst errors = ref({
register: null,
login: null
});
so basically UseUser interface needs to have a definition for errors
and loadings
objects.
also please edit examples to always clear errors for responsible methods:
const register = () => {
errors.register = null // ----> this needs to be added
try {
// ..
} catch (e) {
errors.register = e;
}
};
rest of this looks good to me :)
We need to use this way of handling errors to handle unhandled promises in our factories. e.g. at the end of useLocale and useUser. Otherwise, we will end up with unhandled rejections which are very hard to track.
Changes discussed
- add computed with a flag if there are any errors in composable, example
hasErrors => Object.values(errors).length
- error object parameter has exact the same name as a method
- add
convertError
helper, which can convert caught errors and return string. Example:
try {
// ..
} catch (e) {
errors.register = convertError(e);
}
interface of convertError
will be provided further by @andrzejewsky