【问题标题】:Seeing error message from network tab, but not in the try/catch console.log(error)从网络选项卡中看到错误消息,但在 try/catch console.log(error) 中没有
【发布时间】:2022-01-06 16:41:53
【问题描述】:

我在做一个项目的前端,后端搞定了,基本上满足一个条件,后端就会发送409冲突和具体的错误信息,我的工作就是捕捉那个错误信息并让它显示在前端。

但我无法在 try-catch 块中收到错误消息,只能在 Chrome 的“网络”选项卡中看到错误消息。

这是我的 try-catch 前端代码

try{
            const response = await this.request({
                path: `/api/xxx/xxx/{xxx}/xx`.replace(`{${"xxx"}}`, xx(String(requestParameters.terminalId))),
                method: 'PUT',
                headers: headerParameters,
                query: queryParameters,
                body: UpdateTerminalDataPortRequestDtoToJSON(requestParameters.updateTerminalDataPortRequestDto),
            }, initOverrides);
        }
        catch(e){
            console.log("error message")
            console.error(e)
        }

这是网络选项卡

这是控制台日志错误的样子:

如何获得与网络中显示的相同的错误消息?

在此先感谢....任何评论将不胜感激

【问题讨论】:

  • 尝试将console.log("request resolved") 放在this.request(...) 方法调用之后。如果它会出现并且 409 应该抛出错误,那么您没有正确处理 HTTP 响应代码(例如原生 fetch 解析所有 HTTP 代码)
  • 你已经解决了吗?如果是这样,请告诉我你是如何解决的。

标签: javascript reactjs error-handling frontend


【解决方案1】:

通过catch访问err.response解决了

.catch(function (err) {
    console.log(err.response)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多