• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Exception Handling

#1
[eluser]Treeda[/eluser]
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?

Code:
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->rsegments, 2));


Code:
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->rsegments, 2));
        }
        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

Code:
public function _unhandled( $e){
        
        print "oops";
        
    }

#2
[eluser]m4rw3r[/eluser]
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.