Anyone able to explain a "Undefined variable" error in this PHP code?

Soldato
Joined
15 Nov 2003
Posts
14,395
Location
Marlow
Code:
<?php

class Default_ErrorController extends M5_Controller_Action
{
    public function init()
    {
        $this->_acl()->allow('guest'); // allow guest access to all actions
    }

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        if (strpos($errors->exception->getMessage(), 'SQL Server Native Client')) {
            return;
        }

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

                $request = $this->getRequest();
                $originalController = $request->getParam('controller');
                $originalAction = $request->getParam('action');
                if (strpos($originalController, '.php') || strpos($originalAction, '.php')) {
                    $this->_redirector()->gotoRoute(array('menu_id'=>  App_MenuIds::HOME), 'postSlug');
                    exit;
                }

                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
                break;

            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error';

                Zend_Debug::setSapi('cli');
                if (function_exists('xdebug_disable')) {
                    xdebug_disable();
                }

                $arguments = array();
                $stack = $e->getTrace();                     <-------THIS IS THE LINE!!!
                foreach ($stack as $trace) {
                    $name = $trace['class'] . $trace['type'] . $trace['function'];
                    $arguments[$name] = array();

                    foreach ($trace['args'] as $arg) {
                        if (!is_object($arg)) {
                            $arguments[$name][] = $arg;
                        } else {
                            $arguments[$name][] = get_class($arg);
                        }
                    }
                }
etc...

The error received is:-
Notice: Undefined variable: e in xxxx.php on line 46
Fatal error: Call to a member function getTrace() on a non-object in xxxx.php on line 46​

So is it the fact e isn't defined before that line:-
$stack = $e->getTrace();​

How should it be defined? Or is the code expecting that to have been set elsewhere in the program for that line to then be use when this subroutine is called?

Thanks in advance!
 
Yes, $e isn't defined in your code. How are you expecting it to know what the object is if it hasn't been defined anywhere?
What type of object is it? It either needs to be defined in your class, function or returned by some other function in your code.
 
Ok, I'm not familiar with the framework you're using but you need an exception object. It's possible $errors->exception (used further above) might be what you're after?

Try replacing $e with $errors->exception
 
From the page you linked:

Code:
<?php
  function MakePrettyException(Exception $e) {
    $trace = $e->getTrace();
...
...
...
}
Wouldn't that make
Ok, I'm not familiar with the framework you're using but you need an exception object. It's possible $errors->exception (used further above) might be what you're after?

Try replacing $e with $errors->exception
That looks like a fair idea! I'll give it a go!
 
Back
Top Bottom