How to throw an error from API and catch it from client side?

I have two different projects. First one is the outer shell of the site, basically html+js. There is a registration function where you send data in the POST array:

const registerUser = async function(e) {
 e.preventDefault();

 let email = document.querySelector('#email').value;
 let password = document.querySelector('#password').value;
 let confirmPassword = document.querySelector('#confirm_password').value;

 const formData = new FormData();
 formData.append('email', email);
 formData.append('password', password);
 formData.append('confirm_password', confirmPassword);

 try {
   await fetch(`${API}/users`, {
   method: 'POST',
   body: formData,
   }).then((response) => {
     email = '';
     password = '';
     confirmPassword = '';
     console.log(response);
   })
 }
 catch(err) {
   console.log(err);
 }
}

The second one is a simple REST API on PHP where client side sends data to. It receives the POST array and registers the user.

function registerUser($db, $postData) {
 $email = mysqli_real_escape_string($db, $postData["email"]);
 $password = mysqli_real_escape_string($db, $postData["password"]);
 $confirm_password = mysqli_real_escape_string($db, $postData["confirm_password"]);

 if(empty($postData) || !isset($email) || empty($email) || !isset($password) || empty($password) 
 || !isset($confirm_password) || empty($confirm_password)) return false;

 if($password !== $confirm_password) {
   $_SESSION["error"] = "Passwords don't match!";
   return false;
 }

 $user = mysqliQuery($db, "SELECT * FROM `users` WHERE `email` = '$email';");

 if(mysqli_num_rows($user) > 0) {
   $_SESSION["error"] = 'User with such email already exists!';
   return false;
 };

 $date = date("Y-m-d H:i:s");
 $hashPass = password_hash($password, PASSWORD_DEFAULT);
 $nameFromEmail = strstr($email, '@', true); 

 if(mysqliQuery($db, "INSERT INTO `users` (`id`, `email`, `password`, `registered_at`, `name`) 
 VALUES (NULL, '$email', '$hashPass', '$date', '$nameFromEmail');")) {
   http_response_code(201);
 }

 else {
   http_response_code(401);
 }
}

But the problem is, I don’t know how to throw an error if received email already exists in the database. Is there any way to send an error from PHP server side in response to client side after fetching?