Welcome Guest, Not a member yet? Register   Sign In
Extending CodeIgniter’s Exceptions Class
#1

[eluser]guillermo, out of repose[/eluser]
Just wrote something on the topic, hope it's helpful to some of you guys & gals: http://www.outofrepose.com/2010/02/17/ex...ons-class/
#2

[eluser]packetfox[/eluser]
Hi there,

i have been beating my head against the wall trying to extend the Exception Class; then googling around showed your Website Post which is similar to what i want to do, eg. extending the show_404() (for starters) function so that it will file an entry into a database in addition to logging the error to a file.

Now, no matter what i do, MY_Exceptions.php errors out as soon i do something like $this->CI =& get_instance(); to get the Super Object. After some research, it seems that get_instance() is not available at the time my MY_Exceptions.php calls it. Now i have two questions:

1. How can i load either the Super Object, or at least some additional classes directly from within my extended Exception Class so i can for example access the Database Object?

2. If it does not work for me, how comes it works for you? I mean, sorry if this sounds weird, but for you the Code you posted in your Blog does actually work, and $ci =& get_instance(); does allow you to get to the Superobject, yes?

Thanks and regards!
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    class MY_Exceptions extends CI_Exceptions {

    function __construct(){
        parent::CI_Exceptions();
// Comment the below out and things will stop working
//$this->CI =& get_instance();
    }

    function show_404($page = '') {    
        $heading = "404 Page Not Found";
        $message = "The ppage you requested was not found.";

        log_message('error', '404 Page Not Found --> '.$page);

// Here is where id like to extend so show_404() will also log to a database        
//$this->custom_logging->log_message('error','System','0',"Requested Page not found: $page");

        echo $this->show_error($heading, $message, 'error_404', 404);
        exit;
    }

    }

?>
#3

[eluser]Jelmer[/eluser]
The difference is that he's calling show_404() and show_error() manually and your behavior is probably pre-controller. Once your routing succeeds CI is loaded and you can reference the CI superobject, if your routing fails the CI superobject is never loaded and can't be referenced. Which means that referencing the CI superobject in your Exceptions class is actually a pretty bad idea as it might cause an extra error on its own when an error is thrown pre-controller.
#4

[eluser]packetfox[/eluser]
Hi there,

actually good point! Id still like to log an entry to a database instead of only the log-file each time CI reports a problem though, do you have a good idea on how to accomplish this in a sane and robust way?

Many thanks.
#5

[eluser]Jelmer[/eluser]
I'm still very much figuring that out myself, I'm trying to let all errors be shown using the user-chosen site template - but that's giving me other errors as I use config vars within those views.

The way I'm solving it now:
- Test whether CI has been loaded
Code:
if (class_exists('CI_Base'))
- When CI has been loaded use the CI superobject functions
- When CI hasn't been loaded use the default functions

It's far from perfect, but it's pretty much the best way I can think without doing a lot of work for something that happens very little.

Edit, just a bit more info: when the CI superobject has been loaded and the DB has been succesfully initiated I assume CI itself is working correctly and I redirect to a controller that shows an error. When CI hasn't been loaded I assume the fault is within CI and I show the error in the CI way.




Theme © iAndrew 2016 - Forum software by © MyBB