我是 TypeScript/JavaScript 和 Node.js 的新手,来自 Java/Scala 背景。
我正在编写一个简单的脚本TypeScript来收集一些数据并将它们作为 HTTP POST 请求发送到使用axios.
makePromiseToGetA()
.then((a: A) => makePromiseToGetB(a))
.then((b: B) => sendHttpRequestAxios(b))
.then((resp: AxiosResponse) => outputResponse(resp))
.catch((err: Error) => handleError(err))
现在我想改进错误处理。具体来说,除了通用错误处理(函数)之外,我还想AxiosError使用我的函数来处理handleAxiosError(ae: AxiosError) handleError
现在我看到了两种选择:
像这样修改
handleError函数:// pseudocode because I don't know how to code this in TypeScript function handleError(err: Error): void { if (err instanceof AxiosError) { handleAxiosError(err as AxiosError); } ... // handle Error }"catch"
AxiosErroraftersendHttpRequestAxios,处理错误,然后重新抛出它:makePromiseToGetA() .then((a: A) => makePromiseToGetB(a)) .then((b: B) => sendHttpRequestAxios(b).catch((ae: AxiosError) => {handleAxiosError(ae); throw ae;})) .then((resp: AxiosResponse) => outputResponse(resp)) .catch((err: Error) => handleError(err))
除了使用通用错误处理AxiosError之外,您还建议如何处理?handleAxiosError handleError