title: Handle Error response React.js
body: <p>My Spring boot Controller method:</p>
  
  <pre><code>@RequestMapping(value = "/test",  method = RequestMethod.POST)
      @ResponseBody
      public ResponseEntity&lt;APIResponseMessage&gt; testMethod(@RequestBody MyPojo myPojo) {
          APIResponseMessage resp = new APIResponseMessage();
          try {
              serviceObj.callServiceMethod(myPojo);
              resp.setMessage("successfull!");
          } catch (Exception e) {
              resp.setMessage("failed!");
              return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resp);
          }
          return ResponseEntity.ok(resp);
      }
  </code></pre>
  
  <p>React action handler class has the following method:</p>
  
  <pre><code>export default (data) =&gt; (dispatch) =&gt; {
    dispatch({
      type: initHandler
    });
  
    fetchJSON(url, 'POST', data)
      .then((json) =&gt; {
        dispatch({
          type: successHandler,
          apiResponse: json
        })
      })
      .catch((error) =&gt; {
        dispatch({
          type: failureHandler,
          apiResponse: error,
          apiMessage : "System encountered error. Please try again later."
        })
      })
  }
  </code></pre>
  
  <p>And fetchJSON is define in one of my util classes in react as :</p>
  
  <pre><code>export const checkStatus = response =&gt; {
    const hasError = (response.status &lt; 200 || response.status &gt;= 300)
    if (hasError) {
      const error = new Error("This is an error") // I want to set my message that I obtained from the controller here.
      throw error
    }
    return response
  }
  
  export const parseJSON = response =&gt; response.json()
  
  export const fetchJSON = (url, method, data) =&gt; {
    return fetch(url, {
      method: method,
      headers: new Headers({
        'Content-Type': 'application/json'
      }),
      body: JSON.stringify(data)
    }).then(checkStatus).then(parseJSON);
  }
  </code></pre>
  
  <p>I want to set the custom message that I get from my API to be set to the error object. I tried many options but couldn't make it to work.</p>