Writing a custom error handler

Let’s assume we do not want to show any errors when a non-existing controller is called. The user should simply be redirected to the homepage in such a situation. To accomplish that we have to write a custom error handler.

First, we have to create a file called “error.php” in the “app” directory. We add an “AppError” class to this file which extends the default error handler (cake/libs/error.php) and override the “missingController” function.

class AppError extends ErrorHandler
{
    function missingController($params)
    {
        $this->controller->redirect('/');
    }
}

That’s it? Unfortunately, no. This solution works when we use a debug level > 0, but with a debug level of 0 we get a 404 error. We find the reason for that behaviour in the following lines of the default error handler’s constructor:

if (DEBUG > 0 || $method == 'error') {
    call_user_func_array(array(&$this, $method), $messages);
} else {
    call_user_func_array(array(&$this, 'error404'), $messages);
}

So we have to write our own constructor in our class (fortunately, we can copy almost the entire constructor of the default error handler). Now our AppError class looks like:

class AppError extends ErrorHandler
{
    function __construct($method, $messages) 
    {
        static $__previousError = null;
        $this->__dispatch =& new Dispatcher();

        if ($__previousError != array($method, $messages)) 
	{
            $__previousError = array($method, $messages);

            if (!class_exists('AppController')) 
            {
                loadController(null);
            }
	
            $this->controller =& new AppController();
            $this->__dispatch->start($this->controller);
	
            if (method_exists($this->controller, 'apperror')) 
            {
                return $this->controller->appError($method, $messages);
            }
        } 
        else 
        {
            $this->controller =& new Controller();
        }
			
        call_user_func_array(array(&$this, $method), $messages);
    }
		
    function missingController($params)
    {
        $this->controller->redirect('/');
    }
}

Thanks to PhpNut for the hint with the constructor, and to drSwank for asking in the irc.

10 Comments

  1. Posted June 15, 2006 at 5:02 pm | Permalink

    Thanks for this post. Another option to do this is the following:

    class AppController extends Controller
    {
    function appError($method, $params)
    {
    $this->controller->redirect(‘/’);
    }
    }

  2. Posted June 16, 2006 at 8:58 am | Permalink

    Nice, might came in handy. Just one thing about the layout: With IE 6.0 the content get’s cut off right at the border, I suppose it’s ment to display some sort of scrollbar?

  3. Posted June 16, 2006 at 12:51 pm | Permalink

    @Felix: Thanks for the addition. Something learned :)

    @Konrad: Hm, I am sorry, but here on wordpress I do not have access to the layout.

  4. Posted June 17, 2006 at 10:09 am | Permalink

    Actually, scratch that, I’m not getting a scrollbar in FF (1.5.x) either.

    So in what Browser exactly can you seen the complete Code? :)

  5. Posted June 18, 2006 at 12:46 pm | Permalink

    @Konrad: Well, I use FF 1.5.x without any problems. Anyway, I posted the code as a snippet on cakeforge: http://cakeforge.org/snippet/detail.php?type=snippet&id=79

  6. Posted June 18, 2006 at 2:26 pm | Permalink

    @Konrad: you can always select the code and paste in your editor

  7. Posted August 17, 2006 at 4:33 pm | Permalink

    I’m curious if, after this new Cake update (that takes care of a security vulnerability through the error page), this custom error handler is now flawed as well?

    I had been using this (above) for some time, and it worked great. But after recent cake update & patch (as of Aug 17th) my error page breaks… giving Fatal Error .. in app_controller. Doesn’t seen to be able to call PartyAuth->set() (a custom method we had used).

    Thanks for your time…!

  8. Posted August 19, 2006 at 1:55 pm | Permalink

    @morris: Hm, it works fine for me.

  9. Posted November 17, 2006 at 12:22 am | Permalink

    Thanks for the custom error handler code, just what I have been searching for.

    thanks

  10. Posted November 17, 2006 at 9:31 am | Permalink

    @Bompa: I am glad it is useful for you :)


One Trackback/Pingback

  1. […] cake baker » Writing a custom error handler A very useful error handler for missing controllers. (tags: cakephp controller errorhandler) […]

%d bloggers like this: