Page 1 of 1

Error Handler.

Posted: Thu Aug 17, 2006 2:47 pm
by Jenk
right, thus far I've been simply using similar to:

Code: Select all

<?php

echo "There was an error!";

?>
(not really, but the jist of it is it's very dull and not comparable to the rest of the site.)

So.. I've started designing a class for my error handling. Or should I say, error-message-server. The logging and so forth is fine, but when it comes to showing the user the error message, it sucks.

Why does it suck?

Well, for some errors my View hasn't been instantiated until after, such as authentication error(s) thus there isn't always a View to call/pass.

So..

My error class currently looks like:

Code: Select all

class Error
{
    private static $config;
    
    public static function setConfig ($config)
    {
        self::$config = $config;
    }
    
    public static function setException ($e)
    {    
        self::display($e->getMessage());
    }
    
    public static function setError ($e)
    {
        self::display($e);
    }
    
    private static function display ($msg)
    {
        $view = new jmt_View(new Smarty);
        $view->assignVariable('message', $msg);
        $view->assignVariable('stylesheet', self::$config['STYLESHEET']);
        $view->setTemplate('message.tpl');
        $view->displayPage();
        die;
    }
}
and of course, it's the method display that is just so icky, along with the setConfig method, which is always called in my main controller.

I'm a bit brainfried atm and so I ask for ideas/suggestions from the kind fellows of PHPDN, how would I achieve the same functionality, but in a much lesser maintenance heavy fashion? (e.g. if I change out Smarty for something else I'll need to change not only my View class (which is a wrapper for Smarty) but my Error class as well..)

Much obliged. :)

Posted: Thu Aug 17, 2006 2:55 pm
by feyd
what's "icky" about it?

Posted: Thu Aug 17, 2006 3:04 pm
by Jenk
I don't like having the view instantiation part in there, but then am 'stuck' with it because in some scenarios, I don't have a view object to give to the class. (in most cases infact)

and I've been pondering for the last 20mins or so without any real outcome so thought I would post and ask for opinions whilst I go for a stroll :)

Posted: Thu Aug 17, 2006 3:07 pm
by feyd
You have a registry/service locator pattern object yes? Stick the view in there if it gets that far. This error handler can check get_declared_classes() to see if the Registry is alive, then request the view from it.

How's that sound?

Posted: Thu Aug 17, 2006 8:20 pm
by Jenk
Sounds good, and I'm pondering why I didn't think of it myself.