简体   繁体   English

是4xx和5xx.network错误吗?

[英]Is 4xx and 5xx network errors?

I have a node server that basically reads a component from a specific path, executes the code, and returns the data to the other server.我有一个节点服务器,它基本上从特定路径读取组件,执行代码,并将数据返回给其他服务器。

Sometimes during the code execution of the component, I get a 403 response.有时在组件的代码执行期间,我会收到 403 响应。

Error:错误:

ApolloError: Response not successful: Received status code 403

I'm using .catch() to catch the 403 response but it's not helping and there are frequent pod crashes due to the 403.我正在使用.catch()来捕获 403 响应,但它无济于事,并且由于 403 经常导致 pod 崩溃。

I have checked this StackOverflow answer - Fetch: reject promise and catch the error if status is not OK?我已经检查了这个 StackOverflow 答案 - Fetch: reject promise and catch the error if status is not OK?

It mentions that它提到

Since 4xx and 5xx responses aren't.network errors, there's nothing to catch由于 4xx 和 5xx 响应不是网络错误,因此没有什么可捕获的

Is this correct?这样对吗?

If the above statement is true, can it be handled like the below:如果上面的说法成立,是否可以像下面这样处理:

app.use((req,res) => {
   res.status(403).send('');
})

Is 4xx and 5xx.network errors?是4xx和5xx.network错误吗?

No, they're HTTP error responses, successfully transferred from the server to the client via the.network.不,它们是 HTTP 错误响应,已通过 .network 从服务器成功传输到客户端。

I'm using.catch() to catch the 403 response我正在使用.catch() 来捕捉 403 响应

I assume you mean with fetch given the question you linked to.鉴于您链接到的问题,我假设您的意思是fetch This is a common issue (a footgun in my view ) with using the fetch API. fetch only rejects its promise on network errors , not HTTP errors.这是使用fetch API 的常见问题( 我认为是 footgun) fetch仅拒绝其 promise on network errors ,而不是 HTTP 错误。 So, it will reject its promise if it can't contact the server.因此,如果无法联系服务器,它将拒绝其 promise。 It won't if it successfully contacts the server and receives an HTTP error response.如果它成功联系服务器并收到 HTTP 错误响应,则不会

The answers to the question you link (such as this one ) and my linked blog post above describe how to handle this using a common path for.network and HTTP errors: When you first get the Response object, check its ok property and throw if it's not true, then handle rejection in a common rejection handler.您链接的问题的答案(例如这个)和我上面链接的博客文章描述了如何使用.network 和 HTTP 错误的公共路径来处理这个问题:当您第一次获得Response object 时,检查其ok属性并抛出 if这不是真的,然后在一个常见的拒绝处理程序中处理拒绝。

But reiterating in case those resources disappear (unlikely, but answers are supposed to be self-contained), using JSON as an example, I'll usually have something like this in my codebase on a project:但重申以防这些资源消失(不太可能,但答案应该是独立的),以 JSON 为例,我通常会在我的项目代码库中有这样的东西:

async function fetchJSON(...args) {
    const response = await fetch(...args);
    if (!response.ok) {
        // Network was fine, but there was an HTTP error
        throw new Error(`HTTP error ${response.status}`);
    }
    return await response.json();
}

I do that because the vast, vast , vast majority of the time, the code doing the fetch doesn't care whether there was a.network error or an HTTP error, it just cares that the fetch (and associated JSON parsing) did or didn't work.我这样做是因为在绝大多数情况下,执行fetch代码并不关心是否存在网络错误或 HTTP 错误,它只关心提取(以及相关的 JSON 解析)是否执行或没用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 4XX 和 5XX 错误发送给哨兵? - How to send 4XX and 5XX errors to sentry? 可以在控制台中隐藏资源加载的5xx / 4xx错误吗? - Can 5xx/4xx errors for resource loads be hidden in the console? 在 fetch() Promise 中,当状态为 4xx 或 5xx 时,如何捕获服务器错误消息? - Within a fetch() Promise, how to .catch server errors messages when status is 4xx or 5xx? 当存在4xx或5xx Http错误代码时,在Javascript中解析JSONP响应 - Parsing JSONP Response in Javascript when 4xx or 5xx Http Error Code is Present 5xx 或 4xx 错误,“不存在‘Access-Control-Allow-Origin’标头” - 5xx or 4xx error with “No 'Access-Control-Allow-Origin' header is present” 如何使用 axios 客户端重试 5xx 错误 - How to use axios client to retry on 5xx errors 4xx 状态代码是否应该被视为前端/SSR 应用程序中的错误? - Should 4xx status codes be considered errors in a Frontend/SSR application? 使用then / catch语法通过axios分别捕获4xx和代码错误 - Catching 4xx and code errors separately with axios using then / catch syntax 使用 Redux 工具包异步 REST ZDB974238714CA8DE6FZA 模式时如何捕获 HTTP 4xx 错误? - How to trap HTTP 4xx errors when using Redux Toolkit async REST API pattern? javascript响应未解析为状态码4xx - javascript response not parsed with status code 4xx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM