title: Fetch API: how to determine if an error is a network error
body: <p>So I have some code like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>async function getData() {
    const response = await fetch(/* ... */);
    const json = await response.json();
    return transform(json);
  }
  </code></pre>
  
  <p>Where <code>transform</code> can throw some of its own errors.</p>
  
  <hr>
  
  <p>I'm try to catch for network errors from the <code>fetch</code> API.</p>
  
  <pre class="lang-js prettyprint-override"><code>try {
    const data = await getData();
  
    // ...
    return // ...
  } catch (e) {
    if (isNetworkError(e)) {
      return localStorage.getItem('...');
    }
  
    throw e;
  }
  </code></pre>
  
  <p><strong>My question is how do I implement <code>isNetworkError</code> that works across browsers?</strong> Note: that this should only return true if the network is offline.</p>
  
  <p>It seems like both chrome and firefox throws a <code>TypeError</code> but the messages they have are different on each.</p>
  
  <ul>
  <li>Firefox: <code>TypeError: "NetworkError when attempting to fetch resource."</code></li>
  <li>Chrome: <code>TypeError: Failed to fetch</code></li>
  </ul>