EllisLab text mark
Advanced Search
     
Global Exception Handling
Posted: 14 January 2010 05:46 PM
Joined: 2008-07-28
46 posts

Hi guys,

i just found out that exceptions are not get globally handled.
well for a live application i would love to catch each uncatched exception and forward me as email or write them to a log

i did the following modification to the codeigniter main file.

This change allows you to add a function named “_unhandled” to your controller class.
This methid is called always if an unhandled exception occurs in your controller and you can do what you want.

I just want to know what do you think about this?

Original Code
        
// Call the requested method.
        // Any URI segments present (besides the class/function) will be passed to the method for convenience
        
call_user_func_array(array(&$CI$method), array_slice($URI->rsegments2)); 
Modified Code as a suggestion
        
// Call the requested method.
        // Any URI segments present (besides the class/function) will be passed to the method for convenience
        
try{
            call_user_func_array
(array(&$CI$method), array_slice($URI->rsegments2));
        
}
        
catch( Exception $e ){
            
if ( method_exists(&$CI,"_unhandled") )
                
call_user_func_array(array(&$CI"_unhandled"), $e);
        

in your controllers you can add the exception handler like this

public function _unhandled$e){
        
        
print "oops";
        
    
 
Posted: 16 January 2010 09:29 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-03
671 posts

I would recommend putting the try catch block also around the controller instantiation, as then you can also throw exceptions from eg an auth lib. Finally, if the method_exists() fails, I would have called show_error().

Also, I suggest moving this to the Feature Requests section. But I don’t think it will be supported until CI goes PHP 5 fully.

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser