How to catch 401 error using fetch method of javascript

You can check the status and if it’s not 200 (ok) throw an error

 fetch("some-url")
    .then(function(response)
     {
      if(response.status!==200)
       {
          throw new Error(response.status)
       }
     })
    .catch(function(error)
    {
      ///if status code 401...
    });

Leave a Comment