Posting data via fetch and handling network errors I've built a login panel, which will submit an email and password via fetch/POST. And I wanted to know the best way to handle network status errors?
Reason being, as I want to inject error messages into my login panel for each state/status code.
I've written the code below, which works. But I wondered if there is a better way to write this?  For example, should my status code handling go in the catch() etc?
let url = 'www.MYAPI.com/xyz';

// post body data
let data = {
    &quot;Username&quot;: emailAddress,
    &quot;Password&quot;: Password,
  }

// create request object
let request = new Request(url, {
    &quot;method&quot;: 'POST',
    &quot;mode&quot;: 'cors',
    &quot;credentials&quot;: 'include',
    &quot;body&quot;: JSON.stringify(data),
    &quot;referrerPolicy&quot;: &quot;strict-origin-when-cross-origin&quot;,
    &quot;headers&quot;: {
        &quot;accept&quot;: &quot;application/json, text/plain, */*&quot;,
        &quot;content-type&quot;: &quot;application/json;charset=UTF-8&quot;,
        &quot;sec-fetch-mode&quot;: &quot;cors&quot;,
        &quot;sec-fetch-site&quot;: &quot;same-origin&quot;,
        &quot;x-requested-with&quot;: &quot;XMLHttpRequest&quot;
    }
});

fetch(request)
.then( async (response) =&gt; {
    let data = await response.json();

    if(response.status === 200){
     // LOGIN SUCCESSFUL HERE;
        console.log(&quot;Successful log in - status: &quot;, data);
        document.getElementById(&quot;id01&quot;).style.display = &quot;none&quot;; // HIDE LOGIN PANEL
    } else{
     // Rest of status codes (400,500,303), can be handled here appropriately

        // IF EMAIL ADDRESS ERROR / STATUS 502
        if (data.KnownErrorDescription === &quot;MemberEmailDoesNotExist&quot;) {
            document.getElementById(&quot;email-error&quot;).innerHTML = &quot;Please enter a valid email address to sign in&quot;;
        };

        // IF PASSWORD IS EMPTY / STATUS 400
        if (data.Message === &quot;The request is invalid.&quot;) {
            document.getElementById(&quot;password-error&quot;).innerHTML = data.ModelState[&quot;request.Password&quot;][0];
        }

        // // IF ACCOUNT LOCKED / 502
        if (data.KnownErrorDescription === &quot;MemberAccountLocked&quot;) {
            document.getElementById(&quot;failed-sign-in-error&quot;).innerHTML = &quot;You need to reset your password&quot;;
            document.getElementById(&quot;password-reset&quot;).style.display = &quot;block&quot;;
        }
    };

  })
  .catch((err) =&gt; {
      console.log(err);
  })
I personally think that You can easily handle part of the error handling logic in the catch block however at the moment You are listening for the specific response status do specify what went wrong during the API connection process.
MDN says:

You can use a catch block to handle all exceptions that may be generated in the try block.

try/catch works in a way that if anything fails on the try block, then the rest of the code (in the try block) is ignored and the script execution goes to the catch block. If something goes wrong then the error object is created and passed to the catch block as a parameter .catch(err). From this object You might be able to handle part of the error handling logic. Take a look at the below links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
https://javascript.info/try-catch
Moreover:

data variable can be a const
Instead of writing .style.display = &quot;none&quot;; use YourElement.classList.add('YourNewSexyClass') and use CSS for styling :)
innerHTML is not a good practice. Take a look here -&gt; https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#security_considerations
url variable should a const
data variable should be a const


let data = {
    &quot;Username&quot;: emailAddress,
    &quot;Password&quot;: Password,
  }


can be changed like so (get rid of capital letters here)
let data = {
    username: emailAddress,
    password: password
  }

so that You can change it to
let data = {
    username: emailAddress,
    password
  }