网络错误

模拟请求/网络错误。

你可以使用旨在表示网络错误的 Response.error() 来响应拦截的请求。Response.error() 静态方法返回一种特殊的 Response 实例,它不会被当作普通的服务器响应处理。相反,它会导致网络错误,中止待处理的请求。

¥You can respond to the intercepted request with Response.error() that’s designed to represent a network error. The Response.error() static method returns a special kind of Response insatnce that doesn’t get treated as a normal server response. Instead, it results in a network error, aborting the pending request.

http.get('/resource', () => {
  return HttpResponse.error()
})

使用 Response.error() 方法模拟网络错误,例如:

¥Use the Response.error() method to simulate network errors, such as:

  • DNS 错误;

    ¥DNS errors;

  • 连接超时;

    ¥Connection timeouts;

  • 客户端离线。

    ¥Client going offline.

WHATWG Fetch API 规范没有提供自定义网络错误消息的方法,因此你的客户端将收到通用的 TypeError: Failed to fetch 错误,你应该在请求的 .catch() 闭包中处理该错误。

¥The WHATWG Fetch API Specification provides no way to customize the network error message so your client will receive a generic TypeError: Failed to fetch error that you should handle in the .catch() closure of your request.