When creating a contact getting Error occurred while saving record message

Every time I create a contact this message appears

Error occurred while saving record

Although the record is created successfully.

In the console I get this message in a POST request

Database failure. Please refer to suitecrm.log for details.

{
     "errors": [
         {
             "message": "Internal server error",
             "extensions": { "category": "internal" },
             "locations": [{ "line": 2, "column": 3 }],
             "path": ["saveRecord"]
         }
     ],
     "data": { "saveRecord": null }
}

In the suitecrm logs I get these messages

Query Failed: INSERT INTO contacts_cstm ...

MySQL error 1062: Duplicate entry '859b6192-6572-2f6e-3262-65a40a9da25f' for key 'contacts_cstm.PRIMARY'

And then this:

Error inserting into table: contacts: Query Failed:

MySQL error 1062: Duplicate entry '859b6192-6572-2f6e-3262-65a40a9da25f' for key 'contacts.PRIMARY'

I have commented out hooks that could be causing this behavior but when trying to create a contact again the problem persists. Any ideas?

Thanks in advance

I was able to identify the cause of the error. It has been raised in a before_save hook that does the following

   public function apply($bean, $event, $arguments)
   {
     $emails = $this->getContactEmailAddresses($bean);
     $exist = $this->checkIfAnyEmailExists($emails);

     if (!$exist) {
       $bean->save();
     }
   }

According to the context of this class, an attempt is made to prevent the contact from being saved if a repeated email already exists.

How can I prevent the contact from being saved if it is identified that the contact’s email already exists?

It possible it’s saving the contact, then try to save again, getting “Error occurred while saving record”.