Error Handler.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Error Handler.

Post 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. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's "icky" about it?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Sounds good, and I'm pondering why I didn't think of it myself.
Post Reply