The problem is that Codeigniter 3 sends HTTP 500 status when an error occurs then I can't handle it properly. Here is a code excerpt:
I have a sever side code in PHP:
function delete($customer_id) {
if ($this->db->query("DELETE "
. "FROM _customer "
. 'WHERE customer_id = ?;', array('customer_id' => $customer_id))) {
return ['result' => 'OK'];
} else {
return ['result' => $this->db->error()];
}
}
And the client side is like this:
function deleteRow(customer_id) {
if (confirm("Da li ste sigurni da želite da brišete odabranu stavku?")) {
overlay.style.display = 'block';
webservice({
'proc': 'Customer',
'CallbackFunction': function (r) {
overlay.style.display = 'none';
const json = JSON.parse(r["data"]);
if (json.result === "OK") {
select();
} else {
showToast("Greška", json.result, "error").classList.remove("bg-dark");
}
},
'errorCallbackFunction': function (r) {
overlay.style.display = 'none';
if (r && r.error && r.error.message) {
showToast("Greška 468", r.error.message, "error").classList.remove("bg-dark");
} else {
showToast("Greška 468", "Došlo je do greške molim probajte ponovo ili osvežite stranicu.", "error").classList.remove("bg-dark");
}
},
'data': JSON.stringify({
"function": "delete",
"customer_id": customer_id
})
});
}
};
Where webservice is just a wrapper for javascript fetch function.
And the error is:
<p>Error Number: 1451</p><p>Cannot delete or update a parent row: a foreign key constraint fails (`orange`.`_order`, CONSTRAINT `order_customer_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `_customer` (`customer_id`) ON UPDATE CASCADE)</p><p>DELETE FROM _customer WHERE customer_id = 1;</p><p>Filename: D:/aaa/system/database/DB_driver.php</p><p>Line Number: 691</p> </div>
And because of that HTTP 500 the return statement from delete function never happens in php. I read official documentation but I don't understand how that can work at all...