christianmalek / vuex-rest-api

A utility to simplify the use of REST APIs with Vuex
http://vuex-rest-api.org
MIT License
382 stars 48 forks source link

Handling rejected promises #105

Closed coleman-benja closed 4 years ago

coleman-benja commented 4 years ago

Hello!

"axios": "^0.18.1",
"vuex-rest-api": "^2.10.0",

I'm having an issue where rejected promises are being thrown, despite defining an onError handler.

I'm passing Vapi an instance of Axios that has an interceptor defined like so:

  ax.interceptors.response.use(
    response => response,
    error => {
      // ... do things
      return Promise.reject(error);
    }
  );

and instancing Vapi a la:

const api = new Vapi({
  axios: ax,
  state: {
    posts: [],
  },
})
  .get({
    action: 'getPosts',
    property: 'posts',
    path: ({ postId }) => `/posts/${postId}`,
    onSuccess: (state, payload, axios, { params }) => {
      state.posts = payload.data;
    },
    onError: (state, error, axios, { params, data }) => {
      state.posts = [];
    },
  })
  .getStore();

onError is being called, however it's still reporting Uncaught (in promise) Error. I'm wondering how these could be handled? If I don't return Promise.reject(error) in the interceptor it doesn't trigger onError, but somehow it's still not considered being caught.

christianmalek commented 4 years ago

Hey! Even if you define "onError", it still throws an exception. This is done so that you can still react to single actions outside of Vapi. At the moment you have to do the whole thing with an error handling. But it would be an idea to include this in version 3.


try {
  // success
  const params = { id: 42 }
  const data = { content: "foo" }
  await this.actionName({ params, data })
} catch (error) {
  // error
}

Alternatively use then() and catch().

coleman-benja commented 4 years ago

Ah wasn't sure if it was intended or not, thank you!

christianmalek commented 4 years ago

You're welcome! I'll put that in the documentary. That information is definitely missing.