CodeIgniter Forums

Full Version: Hooking a custom exception handler [SOLVED]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Narkboy[/eluser]
So - I need to display uncaught exceptions in a default view. Most will be catched, but you never know.

I looked at various ways of doing this, but time is a factor so I decied to hook into pre-controller and create MY_Exception.php in application/hooks:

Hook:
Code:
$hook['pre_controller'] = array(
    'class'        => '',
    'function'    => 'set_my_exception_handler',
    'filename'    => 'MY_Exception.php',
    'filepath'    => 'hooks'
);

application/hooks/MY_Exception.php:
Code:
function my_exception_handler( $e ) {

    echo 'Uncaught exception'; // To test we got here. We do.
    
    $_ci =& get_instance(); // CI super object to access load etc.

    $data['crumbs'] = array(); // For nav
    $data['crumbs'][] = array( 'Error' , '' ); // Nav contd
    $data['menu'] = $_ci->uri->rsegments[1] . '/' . $_ci->uri->rsegments[2]; // Menu highlight

    $data['snip'] = 'admin/snip/exception_view'; // The inner view to load
    $data['message'] = $e->getMessage(); // The exception text

    $_ci->load->view( 'admin/base3' , $data ); // The main view

}

function set_my_exception_handler() {

    set_exception_handler('my_exception_handler');

}

So - we declare the exception function, then use the hook to call set_my_exception_handler which defines the preceeding function as the handler. So far so good.

That all works - what does not work is actually loading the view.

Controller:
Code:
function help_test() {

        $this->load->model('help_admin');

        $this->help_admin->delete_link('help_articl' , 'article_id_1' , 1 );
    }

Now - that call to help_admin is going to throw an expception. I *should* see echo'd 'Uncaught Exception' followed by the view. All I do see is 'Uncaught Exception'. With errors set to E_ALL and logging of 'all messages', I get no error and the log shows:

Code:
DEBUG - 2010-12-09 14:40:41 --> Config Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Hooks Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Unicode Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Unicode Class - UTF-8 Support Enabled
DEBUG - 2010-12-09 14:40:41 --> URI Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Router Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Output Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Input Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Security Class Initialized
DEBUG - 2010-12-09 14:40:41 --> CRSF cookie Set
DEBUG - 2010-12-09 14:40:41 --> Global POST and COOKIE data sanitized
DEBUG - 2010-12-09 14:40:41 --> Language Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Loader Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Config file loaded: ../htapp/application/config/tgt_config.php
DEBUG - 2010-12-09 14:40:41 --> Helper loaded: url_helper
DEBUG - 2010-12-09 14:40:41 --> Helper loaded: gatekeeper_helper
DEBUG - 2010-12-09 14:40:41 --> Helper loaded: form_helper
DEBUG - 2010-12-09 14:40:41 --> Helper loaded: ct_helper
DEBUG - 2010-12-09 14:40:41 --> Database Driver Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Session Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Helper loaded: string_helper
DEBUG - 2010-12-09 14:40:41 --> Encrypt Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Session routines successfully run
DEBUG - 2010-12-09 14:40:41 --> Model Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Model Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Model Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Controller Class Initialized
DEBUG - 2010-12-09 14:40:41 --> Model Class Initialized
DEBUG - 2010-12-09 14:40:41 --> File loaded: ../htapp/application/views/admin/snip/exception_view.php
DEBUG - 2010-12-09 14:40:41 --> File loaded: ../htapp/application/views/admin/base3.php

Which suggests that the views are loaded. So - whats going on? Using the exact same syntax I can load and 'see' every other page on the site - it's worked for the last god knows how many view files. But not here. :question:

Help!!


**Edit 1:**

Just checked the full log for a successfull page and it contains 2 further log items:
Code:
DEBUG - 2010-12-09 14:48:24 --> Final output sent to browser
DEBUG - 2010-12-09 14:48:24 --> Total execution time: 0.2850

So - clearly, load->view isn't running all the way....

**Edit 2:**

If I load the views as data and echo it, it works. The {elapsed_time} isn't shown correctly, but otherwise it's fine..

**Edit 3:**

RTFM.

Quote:Views are never called directly, they must be loaded by a controller.

So - that's why the view isn't loading. Good...