Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/core/rfcs/errors-habdling-in-composables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Errors handling in composables

## Motivation

We need to handle errors, that are caused by external api calls provided by factory params.
Furthermore, it must be something to recognize where that error comes from (which call).

We can solve it by introducing fields inside the errors object and the `AgnosticError`:

Factory
```js
const useProductFactory = (factoryParams) => {
const useProduct = () => {
const products = ref([]);
const errors = ref({};)


const search = async (params) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const search = async (params) => {
const search = async (params) => {
errors.searchProducts = null

try {
products.value = factoryParams.searchProducts(params);
} catch (err) {
errors.searchProducts = factoryParams.convertError(err);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params should also be passed to the converError in order to supply additional information about context of this method used.

If we execute one method multiple times we will lose all errors except the one from last execution

}
}


return { products, search }
}
}

export default useProductFactory
```

Usage:

```js
import { useProductFactory } from '@vue-storefront/factories';


const factoryParams = {
searchProducts: async (params) => {
return await getProductsFromApi(params);
},
convertError: (error) => {
if ('this is search product error ?') {
return 'this is error message for searching products';
}

return error;
}
};

export default useProductFactory()

```

Summary:
- factory catches errors, and convert them to the readable message using `convertError`
- `convertError` is a new factory param, you have to provide it in order to handle and react on errors


## Migration process
Transparent, `convertError` is optional, also you don't have to implement errors if you don't need now