Axios捕获Promise错误

时间:2017-10-23 17:55:09

标签: reactjs axios

我有以下axios电话:

axios.put({API_IMAGE_URL}}`, imageData, { withCredentials: true, headers: { 'Content-Type': 'multipart/form-data' } })
                .then(response => {
                    axios.get(`${API_URL}/images/${response.data.id}`, { withCredentials: true, headers: { 'Content-Type': 'multipart/form-data' } })
                        .then(response => {
                            var newFormat = response.data;
                            dispatch(updateFormat(newFormat))
                        });
                }).catch((e) => {
                    console.log("Can't update image ", e);

基本上我有一个传递给url的图像对象。从那里,我需要检索新的Image对象及其用于其他事物的新ID。

如果我的图片太大,我会出现以下错误:

413 (Request Entity Too Large)
Uncaught (in promise) Error: Network Error 

我的问题在于此。它给了我这些错误,并没有去catch条款。有没有办法捕捉这些承诺错误?

1 个答案:

答案 0 :(得分:1)

只需返回第二个axios承诺

    axios.put({API_IMAGE_URL}}`, imageData, { withCredentials: true, headers: { 'Content-Type': 'multipart/form-data' } }).then(response => {
        // Return the promise from here like this
        return axios.get(`${API_URL}/images/${response.data.id}`, { withCredentials: true, headers: { 'Content-Type': 'multipart/form-data' } }).then(response => {
            var newFormat = response.data;
            dispatch(updateFormat(newFormat))
       });
   }).catch((e) => {
       console.log("Can't update image ", e);