title: How to throw a server error when fetching JS
body: <p>I'm new in JavaScript and i have a task to post an email input from form to a node server,everything works fine,but i should implement this functionality:<br />
  When an email is forbidden@gmail.com, the server responds with the 422 status code and payload which contains the information about the error. Use browser developer tools to examine the response for this scenario. Display the error message in the browser using window.alert().<br />
  I created a customException,it gives me the error in the console,but the server still responds with the 200 status code,but as i understand,it should give an error and the post should not work.How to do this task,i have no idea..?<br />
  <strong>Fetch functions:</strong></p>
  <pre><code>import { validateEmail } from './email-validator.js'
  
  export const sendSubscribe = (emailInput) =&gt; {
      const isValidEmail = validateEmail(emailInput)
      if (isValidEmail === true) {
          sendData(emailInput);
          // if (emailInput === 'forbidden@gmail.com'){
          //     throw new CustomException('422');
          // }
      }
  }
  
  const sendHttpRequest = (method, url, data) =&gt; {
      return fetch(url, {
          method: method,
          body: JSON.stringify(data),
          headers: data ? {
              'Content-Type': 'application/json'
          } : {}
      }).then(response =&gt; {
          if (response.status &gt;= 400) {
              return response.json().then(errResData =&gt; {
                  const error = new Error('Something went wrong!');
                  error.data = errResData;
                  throw error;
              });
          }
          return response.json();
      });
  };
  
  const sendData = (emailInput) =&gt; {
      sendHttpRequest('POST', 'http://localhost:8080/subscribe', {
          email: emailInput
      }).then(responseData =&gt; {
          console.log(responseData);
      }).catch(err =&gt; {
          console.log(err, err.data);
      });
  }
  
  function CustomException(message) {
      const error = new Error(message);
      error.code = &quot;422&quot;;
      window.alert('Forbidden email,please change it!')
      return error;
    }
    
    CustomException.prototype = Object.create(Error.prototype);
  </code></pre>
  <p>Validate function:</p>
  <pre><code>const VALID_EMAIL_ENDINGS = ['gmail.com', 'outlook.com', 'yandex.ru']
  
  export const validateEmail = (email) =&gt; !!VALID_EMAIL_ENDINGS.some(v =&gt; email.includes(v))
  
  export { VALID_EMAIL_ENDINGS as validEnding }
  </code></pre>
  <p>Please help.Thanks in advance!</p>