gpt4 book ai didi

javascript - 从 API 获取 - 特定 API 错误与一般 HTTP 错误

转载 作者:行者123 更新时间:2023-12-02 23:04:00 24 4
gpt4 key购买 nike

我正在使用 fetch() 从 API 获取结果。据我了解,有两种不同类型的错误:

  • 我的 API 特别返回的错误:例如“无效的身份验证 token ”
  • 更常见的 HTTP 错误:例如 - 网络超时或连接中断。

有没有办法确定问题的性质,以便我可以在有意义时重试 API 命令(例如网络连接不良),但如果它只是给出相同的结果,则不重试与上次一样(例如无效 token 错误)

最佳答案

Is there a way to determine the nature of the issue, in such a way that I can retry an API command when it makes sense (like poor network connectivity), but not retry it if it's just going to give the same result as the previous time (like an invalid token error)

fetch() 仅在接收数据时出现错误时抛出。如果您的服务器响应错误状态,您必须在 then() block 中进行检查。

@Thomas Thanks! So in general if i enter my catch() block of the fetch(), I can treat that as a network issue?

换个 Angular 思考。如果你到达then()部分,你的服务器已经响应成功;您是否喜欢该回复(200)或不(404)

如果您进入 fetch()catch() 部分,则请求出现问题,而网络错误是最可能的原因之一。无效的 url,例如 "//",将是抛出的另一个原因。

关于javascript - 从 API 获取 - 特定 API 错误与一般 HTTP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57670130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com