![]() |
Database functionality and Exceptions - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Database functionality and Exceptions (/showthread.php?tid=2098) |
Database functionality and Exceptions - El Forum - 07-16-2007 [eluser]Al James[/eluser] Hi there... Just a thought. In my old framework (self built) when a database query failed, it threw an exception and let the try and catch blocks attempt to recover. CI on the other hand simply displays a message and exits. What do people think is the better behaviour? Personally I feel throwing an Exception is best, as if no try / catch blocks are used, it will be picked up by the default handler and exit anyway... To do this, I changed the display_error function of the DB_driver class: Code: function display_error($error = '', $swap = '', $native = FALSE) Its not very nice, a hack! In fact, it appears that CI does not use Exceptions very well at all. It would be much nicer to have CI_Exception as a throwable object! Database functionality and Exceptions - El Forum - 07-16-2007 [eluser]champs[/eluser] Forget about try... catch, as long as there is PHP4 support. Database functionality and Exceptions - El Forum - 07-16-2007 [eluser]Al James[/eluser] Urg! Yes, I forgot about Exceptions not being in php4. Hmmmm... I wonder if there is away around this? I.e. Have the CI_Exceptions class actually throw an Exception if its running on PHP5? Like this: * Create a CI_Exception class that extends Exception and holds the fields from CI_Exceptions->show_error * Create a default exception handler that receieves the CI_Exception and does the same as the current CI_Exceptions->show_error function. * Change CI_Exceptions->show_error so that if PHP5, throw a new Exception, if PHP4 just do as normal. I understand that having 'throw' etc in the code igniter libraries would break PHP4 support. You could either: A) Have a separate file for PHP4 and 5 (like Base4.php and Base5.php files) B) eval('throw new Exception()'); (sneaky hey?) What do ya all think? Database functionality and Exceptions - El Forum - 07-16-2007 [eluser]Al James[/eluser] Or, how about something like this: Code: function throw_exception($exception_class, $message) You could then throw Exceptions by: Code: throw_exception('Exception', 'An error occurred'); Not tested this in php4, but it should work! (I think!) Database functionality and Exceptions - El Forum - 10-18-2007 [eluser]Jay Callicott[/eluser] I like where you're going, myself I don't care about php 4 support but I would like it to throw exceptions if db_debug is set to FALSE, otherwise it can show the pretty error message. I think a hack is probably the way to go. Can't fully follow your last example, why do you need evals? Database functionality and Exceptions - El Forum - 10-18-2007 [eluser]Al James[/eluser] Looking at my code only the first eval needs to be there. Code: function throw_exception($exception_class, $message) The first eval is needed as otherwise the code would not run on php4 (throw is not a recognised language construct)... Database functionality and Exceptions - El Forum - 10-18-2007 [eluser]Jay Callicott[/eluser] This isn't exactly a global fix for all but something I might add to my shared install of CI is: Code: if ($CI->config->item('db_exception_handling') && $template == 'error_db') { In the show_eror function in Exceptions.php. Unfortunately you can't override Exceptions and so I would have to hack it which is annoying bc it's one more thing I have to document so that I don't mess things over when I upgrade CI. I've put in config values before is some of my libraries and thus far they've worked well for me. I am all php 5 and I would want to use this so that I can catch certain db errors. This is tricky because if I really don't want to do a try catch for every db call I make, only the ones that are prone to db errors, in my case mssql queries are prone to timeouts and foreign key constraints cause more failures in queries now that im using foreign keys alot. Darn this is tricky. I basically would want it to catch any uncaught exceptions with a nice pretty error message bc I'm not going to add a try/catch block to every single query. Hmmm. Database functionality and Exceptions - El Forum - 10-18-2007 [eluser]Al James[/eluser] Just do this! ![]() Code: function default_exception_handler($e) |