Fatal Errors when DataBase command fails |
Hello all, I'm having a struggle with Fatal Errors being thrown when some of my Databases commands don't return successfully, for example:
Code: $flag = true; In my Database (Postgres) I created an UNIQUE KEY for the column "name". So when I try to insert a name that already exists I would like to get a return of none "affectedRows", and then deal with this return in my function, but when the error occurs, I get a Fatal Error, my request returns instantly with code 500 and an error description that follows: Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "Type is not supported". in /var/www/html/mysite/system/Format/Exceptions/FormatException.php:9 I could create an SELECT to search for the name before the INSERT, but I'm planning in creating some control in my database using UNIQUE KEYS for a better setup. Thanks in advance
(10-18-2019, 10:14 AM)titounnes Wrote: replace "insert" with "save" Got this error (method "save" does not exist): "title": "Error", "type": "Error", "code": 500, "message": "Call to undefined method CodeIgniter\\Database\\Postgre\\Builder::save()", "file": "/var/www/html/mysite/app/Models/AudiosModel.php", "line": 33, "trace": [ Remembering: I'm using CodeIgniter 4
Instead of checking for the number of affected rows, catch the Exception that's being thrown. You can handle the same situation you're trying now within the catch block.
(10-18-2019, 01:41 PM)kilishan Wrote: Instead of checking for the number of affected rows, catch the Exception that's being thrown. You can handle the same situation you're trying now within the catch block. I was already using an Try Catch when I call this function (it is inside a Modal). But I tried to make the Try Catch just around the Insert method or all the my $db function but it didn't prevent the request from being shut down and return status 500 to my browser. I tried catch(Exception $ex){... and catch(FormatException $ex){ Here is what is happening to the request: (repair the echo "Hello World 1!!\n" and echo "Hello world 2!!\n") http://prntscr.com/pl9oub And here is the code I'm running: Code: public function addAudio($name, $path){ |
Welcome Guest, Not a member yet? Register Sign In |