title: How to read error messages from javascript error object
body: <p>Could some one help me out on below question please :-)</p>
  <p>I'm making a post call through redux action, which is below.</p>
  <pre><code>export const addEmployee = ({ firstName, surname, contactNumber, email }) =&gt; async dispatch =&gt; {
  const payloadBody = JSON.stringify({ firstName, surname, contactNumber, email });
      fetch('/api/users', { 
              method: 'POST', 
              body: payloadBody,
              headers: {
                  'Content-Type': 'application/json'
              }
          })
          .then(response =&gt; {
              if (!response.ok) {
                  return response.text()
                  .then(text =&gt; { 
                      throw Error(text)
                  });
              } else {
                  dispatch(setAlert(&quot;New Employee added &quot;, 'danger'));
              }
          })
          .catch(error =&gt; {
              console.log('&gt;&gt;&gt; in CATCH block, error is =&gt;', error);
              console.log('&gt;&gt;&gt; in CATCH block, error name is =&gt;', error.name);
              console.log('&gt;&gt;&gt; in CATCH block, error message is =&gt;', error.message);
  
              let allKeys = Object.getOwnPropertyNames(error);
              console.log(allKeys); 
  
              // const errors = [];
              // Object.keys(error.message).forEach(key =&gt; {
              //     console.log('&gt;&gt;&gt; key are ', key)
              // })
  
  
              // const keys = Object.keys(error.message);
              // console.log(keys);
  
              // const errors = error.message['errors'];
              
              
              // const errors = error.response.data.errors;
  
              // if (errors) {
              //     errors.forEach(error =&gt; dispatch(setAlert(error.msg, 'danger')));
              // }
  
              dispatch({
                  type: REGISTER_FAIL
              });
          })
  }
  </code></pre>
  <p>Above post call on failure, returns body with error message, an example is below</p>
  <pre><code>    {
      &quot;errors&quot;: [
          {
              &quot;msg&quot;: &quot;User already exist with email&quot;
          }
       ]
  }
  </code></pre>
  <p><strong>Question</strong>
  What I'm trying to achieve is, to grab the <code>errors[]</code> and pass the error message through to a component, the trouble I have is accessing the <code>error[]</code> array within the returned array message. I'll describe below what I've attempted, it also can be seen in the redux action method I posted above.</p>
  <p><strong>Try-1</strong>
  <code>console.log('&gt;&gt;&gt; in CATCH block, error is =&gt;', error);</code> yields just <code>Error</code></p>
  <p><strong>Try-2</strong>
  <code>console.log('&gt;&gt;&gt; in CATCH block, error name is =&gt;', error.name);</code> yields <code>{&quot;errors&quot;:[{&quot;msg&quot;:&quot;User already exist with email&quot;}]}</code> and the typeof this is <code>string</code> since I'm returning text() <code>return response.text().then(text =&gt; { throw Error(text) })</code></p>
  <p><strong>Try-3</strong></p>
  <p>When I return as json() <code>return response.json().then(text =&gt; { throw Error(text) })</code>and <code>console.log('&gt;&gt;&gt; in CATCH block, error message is =&gt;', error.message);</code> yields object.</p>
  <p>The questions again What I'm trying to achieve is, to grab the <code>errors[]</code> and pass the error message through to a component such as below</p>
  <pre><code>            const errors = error.message; // this is where I'd like to extract the error.
  
               if (errors) {
                   errors.forEach(error =&gt; dispatch(setAlert(error.msg, 'danger')));
               }
  </code></pre>
  <p>Hope the above description is clear, please let me know if you require more info,
  I know I'm missing some crucial knowledge of working with error objects, could someone please shed some light on this please :-)</p>