title: How can I pass JSON body of Fetch response to Throw Error() with then()?
body: <p><strong>EDIT:</strong> You've misunderstood. Consider this psuedo code. This is essentially what I want to do but it doesn't work when wrote like that.</p>
  
  <p>Once you receive a Laravel 422 response with Fetch, the <code>response</code> doesn't contain the actual JSON data. You have to use <code>response =&gt; response.json()</code> to access it. I've put a picture of what the <code>response</code>holds and I've also supplied the output after using <code>response =&gt; response.json()</code>. </p>
  
  <p>I am <strong>NOT</strong> trying to convert an object into a JSON string. </p>
  
  <p>I'm using SweetAlert2 with Laravel. </p>
  
  <p><code>throw error(response =&gt; response.json())</code> </p>
  
  <pre><code>swal({
    title: 'Are you sure you want to add this resource?',
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Submit',
    showLoaderOnConfirm: true,
    allowOutsideClick: () =&gt; !swal.isLoading(),
    preConfirm: () =&gt; {
      return fetch("/resource/add", {
          method: "POST",
          body: JSON.stringify(data),
          credentials: "same-origin",
          headers: new Headers({
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
            'Content-Type': 'application/json',
            'Accept': 'application/json'
          })
        })
        .then(response =&gt; {
          if (!response.ok) {
  
            // How can I return the response =&gt; response.json() as an error?
            // response.json() is undefined here when used
  
            // throw Error( response =&gt; response.json() ); is what I want to do
            throw Error(response.statusText);
  
          }
          return response;
        })
        .then(response =&gt; response.json())
        .then(res =&gt; console.log('res is', res))
        .catch(error =&gt; console.log('error is', error));
    }
  })
  </code></pre>
  
  <p><code>response</code> holds the following after <code>response =&gt; response.json()</code> and it's this JSON that I want to pass to <code>error()</code>.</p>
  
  <pre><code>{"message":"The given data was invalid.","errors":{"name":["The name field is required."],"abbrev":["The abbrev field is required."]}}
  </code></pre>
  
  <p>I make an AJAX call and if Laravel's validation fails, it returns a <code>status 422</code> with the validation error messages in JSON. However, Fetch doesn't count <code>422</code> responses as an 'error' so I have to manually trigger it myself with <code>throw</code>. </p>
  
  <p>Because my validation messages are in the <code>JSON</code> response i.e.
  <code>then(response =&gt; json())</code>
   how can I convert my <code>response</code> to <code>json</code> and then pass it to my <code>throw error()</code> ?</p>
  
  <p>Just FYI, this is what <code>console.log(response)</code> holds before using <code>response =&gt; response.json()</code>
  <a href="https://i.stack.imgur.com/D6Rnb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D6Rnb.png" alt="enter image description here"></a></p>