Catch database exception |
Hi,
I have some problems with catching a database exception. On the development, everything works as I expected but not on the production. My code looks like below: PHP Code: try { On the development environment in the logs file, I have only `Duplicate status...` - as I want On the production environment in the logs file, I have a raw exception PHP Code: ERROR - 2020-10-13 20:25:22 --> mysqli_sql_exception: Duplicate entry '441-2' for key 'order_id_status_id' in /var/www/mrsystem/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php:331 If I changed in the Database config variable DBDebug to TRUE then I have a raw exception and logged a warning PHP Code: ERROR - 2020-10-13 20:25:22 --> mysqli_sql_exception: Duplicate entry '441-2' for key 'order_id_status_id' in /var/www/mrsystem/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php:331 Can someone tell me what I am doing wrong? How to on the production environment get an only logged warning?
You can try it using CodeIgniters Exceptions.
PHP Code: try What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Unfortunately, what you propose doesn't help :/
EDIT: I cannot use `exit($e->getMessage());` because the executed code is stopped
I was surfing the web and came across this maybe it will help you out.
Code: How to use try catch with MySQL This may help others out also. After looking at the MySQLi connection it is not setting these up in CodeIgniter. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I found in system/Database/MySQLi/Connection.php:329 this code:
PHP Code: try Maybe I'm wrong but, if we have DBDebug sets true then we shouldn't call log_message? So I mean that log_message should be inside else. What do you think?
No, because it needs to log the message before it invokes the exception error.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
But what is a sens to log an error if we have enabled database throws for debug mode? In this situation, every time an error is logged even if we catch it manually and do something.
|
Welcome Guest, Not a member yet? Register Sign In |