W3cubDocs

/Web APIs

Response: error() static method

The error() static method of the Response interface returns a new Response object associated with a network error.

This is mainly useful when writing service workers: it enables a service worker to send a response from a fetch event handler that will cause the fetch() call in the main app code to reject the promise.

An error response has its type set to error.

Syntax

js

Response.error()

Parameters

None.

Return value

A Response object.

Examples

Returning a network error from a service worker

Suppose a web app has a service worker, which contains the following fetch event handler:

js

// service-worker.js

self.addEventListener("fetch", (event) => {
  const url = new URL(event.request.url);
  if (url.pathname === "/salamander.jpg") {
    event.respondWith(Response.error());
  }
});

With this service worker, all fetch requests from the app will pass through the service worker to the network, except for requests to fetch "salamander.jpg", which will reject. This means that the following main thread code would throw an error, and the catch handler will run.

js

// main.js

const image = document.querySelector("#image");

try {
  const response = await fetch("salamander.jpg");
  const blob = await response.blob();
  const objectURL = URL.createObjectURL(blob);
  image.src = objectURL;
} catch (e) {
  console.error(e);
}

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
error_static 43 16 39 No 30 10.1 43 43 39 30 10.3 4.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Response/error_static