A
A
Anton Ivanov2019-10-04 00:39:08
JavaScript
Anton Ivanov, 2019-10-04 00:39:08

How to properly organize error handling logic?

Hello.
There is a SPA on vue.js.
There is a wrapper for API calls. For some error codes, the reaction should always be unambiguous (just displaying a message with the error text), for other codes there should be an individual reaction depending on ...
I want the error message to show the wrapper, and if the error requires individual processing, then not would do nothing.
All communication with the API is based on promises.
Elementary example:

this.$apiImpl.users.index()
.then(response => {
  //....
})
.catch(error => {
  //....
})
.finally(() => {
  //....
})

inside $api.index() happens
axios.get(...)
.catch(error => {
  if (!...)  return Promise.reject(error); // если ошибка обрабатывается индивидуально

  // делаем все, что нужно (показываем сообщение)
  return Promise.reject('error_handled')
})

in this approach wildly do not like the fact that in any catch'e, you have to check, if ('error_handled' === error) return;
Is there any beautiful way to solve this?
It is possible to return an unresolved promise from wrapper, then execution will not get into catch at all (permissible), but then finally will not work either (unacceptable).
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question