【发布时间】:2020-05-16 01:41:28
【问题描述】:
我有两个应用程序(appA、appB)。 A呼叫B
AppA 端点是
api.get('/', (req, res, next) => {
return axios({
method: "GET",
url: "http://localhost:3060/api/"
}).then((data) => {
return data.data
}).catch((err) => {
const {config, request, response} = err
let e = new Error(`Rethrowing the "${"B"}" error FROM A`)
e.stack?.split('\n').slice(0,2).join('\n') + '\n' + response.data;
throw e
})
});
AppB 是:
api.get('/', (req, res) => {
throw new Error('B')
});
我的目标是将 AppB 中的错误附加到 AppA 中的新错误中,并将其发送给调用 AppA 的人。当我尝试从 axios 调用中抛出错误时,控制台中打印出错误并显示nhandledPromiseRejectionWarning: Error: Rethrowing the "B" error FROM A。并且端点只是超时。
如何正确抛出 Axios Catch 中的附加错误?我想发送带有错误消息的堆栈跟踪。
谢谢
【问题讨论】:
标签: node.js express error-handling axios