0

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...

Dejan Dozet
  • 948
  • 10
  • 26
  • did you try to discover the error? *a foreign key constraint fails*, e.g. https://stackoverflow.com/questions/43493889/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails-mysql or https://stackoverflow.com/questions/1905470/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails – Vickel Jun 19 '22 at 15:08
  • @Vickel no, that is not the point, the point is when such an error occurs to be able to pick it with Codeigniter and show it in the browser. – Dejan Dozet Jun 19 '22 at 15:10
  • 1
    in my experience, since you are using ajax/json, you should see the corresponding 500 error message (most likely a database error) in the browser's network tab ? wouldn't that be enough for debugging? – Vickel Jun 19 '22 at 15:14
  • @Vickel you are the king! Thanks for staying with me on this. I realized that the problem was caused by Codeigniter developer mode, I've switched to the production and I am getting these errors now! – Dejan Dozet Jun 19 '22 at 17:49
  • great to hear you get your error messages, BUT: production environment should hide most of errors, unlike development environment. so your self-answer might not be correct and only work as a coincidence to your problem. – Vickel Jun 19 '22 at 21:49
  • it is perfectly clear, look, during development, all errors are thrown as an HTML page, but in production, they are not – Dejan Dozet Jun 20 '22 at 13:54

1 Answers1

0

I found what was causing this! I totally forgot that the site was running on the developer mode, after I switched it to the production mode error came through as they should!

So in index.php, just change:

define('ENVIRONMENT', 'development');

to

define('ENVIRONMENT', 'production');
Dejan Dozet
  • 948
  • 10
  • 26