title: Rejected Fetch Promise: access to response text
body: <p>I’m using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="nofollow noreferrer">Fetch API</a> to load some JSON. Therefore, I use <a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/json" rel="nofollow noreferrer"><code>.json()</code></a> on the response stream and also the optional second function argument (<code>onRejected</code>) in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then" rel="nofollow noreferrer"><code>.then()</code></a>, which has only one argument, the rejection reason.</p>
  
  <p>If something goes wrong (like the response being no valid JSON), I want access to the raw response text to have some meaningful error log or to further process the unexpected response.</p>
  
  <p>Consider this simplified snippet:</p>
  
  <pre class="lang-javascript prettyprint-override"><code>fetch('api.php')
  .then(
      response =&gt; { return response.json(); }
  )
  .then(
      json =&gt; { /* useful fulfillment value, a JSON object */ },
      reason =&gt; { /* string message only */ }
  );
  </code></pre>
  
  <p>If the API responds with something other than valid JSON, it will reject with the following reason:</p>
  
  <blockquote>
    <p><em>SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data</em></p>
  </blockquote>
  
  <p>While this is a perfectly clear error message, it lacks a dump of what actually <em>caused</em> the error.</p>
  
  <p>I considered using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/text" rel="nofollow noreferrer"><code>.text()</code></a> and then trying to parse the JSON myself, but that would somewhat contravene the purpose of <code>.json()</code>.</p>
  
  <p><strong>Is there a reasonable way to access the response text if stream-reading is rejected?</strong></p>