Express 无法从Axios获取正确的错误

Express 无法从Axios获取正确的错误,express,axios,Express,Axios,我有一个处理所有api调用的doFetch函数: const doFetch = function(params){ ... // Make request using Axios. Axios is promise based. return axios({ method: method, url: baseUrl + url, data: queryParams, timeout: timeout,

我有一个处理所有api调用的
doFetch
函数:

const doFetch = function(params){
    ...
    // Make request using Axios. Axios is promise based.
    return axios({
        method: method,
        url: baseUrl + url,
        data: queryParams,
        timeout: timeout,
        headers: {
            'Content-Type': contentType,
            'Authorization': `bearer ${Auth.getToken()}` // set the authorization HTTP header
        },
        responseType: responseType
    }).then((response) => {
        if(typeof params.callback === "function"){
            params.callback(response);
        }
        else {
            return response;
        }
    }).catch((err) => {
        if(typeof params.error === "function") {
            if (err.response) {
                params.error(err.response.data);
            }
        }
        else{
            if (err.response) {
                return err.response.data;
            }
            else{
                return err;
            }
        }
    });
};
一个这样的api调用返回一个自定义错误,如so(express server):

调用doFetch的函数是
saveUser

saveUser(userObj).then((response) => {                
    console.log("No Error");
}).catch((error) => {
    console.log("Error:", error);
});

问题是,我在终端中看到
无错误
,而我本应只希望显示错误消息。有什么想法吗?

我喜欢准确地回报承诺,以确保它确实/回报了我想要的东西

我不喜欢依赖第三方的“承诺”


因此,我建议您将其包装在promise中,并手动解决/拒绝响应/错误:

const doFetch = params => {
    ...
    // Make request using Axios. Axios is promise based.
    return new Promise((resolve, reject) => {
      axios({
        method: method,
        url: baseUrl + url,
        data: queryParams,
        timeout: timeout,
        headers: {
            'Content-Type': contentType,
            'Authorization': `Bearer ${Auth.getToken()}` // set the authorization HTTP header
        },
        responseType: responseType
      })
      .then((response) => {
        console.info('doFetch:', response); // for debug purposes

        if(typeof params.callback === "function"){
          params.callback(response);
        }
        resolve(response);
      })
      .catch((err) => {
        console.error('doFetch:', err); // for debug purposes

        const error = (err.response) ? err.response.data : err;

        if(typeof params.error === "function") {
          params.error(error);
        }
        reject(error);
      });
    };
};

非常感谢。
const doFetch = params => {
    ...
    // Make request using Axios. Axios is promise based.
    return new Promise((resolve, reject) => {
      axios({
        method: method,
        url: baseUrl + url,
        data: queryParams,
        timeout: timeout,
        headers: {
            'Content-Type': contentType,
            'Authorization': `Bearer ${Auth.getToken()}` // set the authorization HTTP header
        },
        responseType: responseType
      })
      .then((response) => {
        console.info('doFetch:', response); // for debug purposes

        if(typeof params.callback === "function"){
          params.callback(response);
        }
        resolve(response);
      })
      .catch((err) => {
        console.error('doFetch:', err); // for debug purposes

        const error = (err.response) ? err.response.data : err;

        if(typeof params.error === "function") {
          params.error(error);
        }
        reject(error);
      });
    };
};