We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Intercepting errors in a multi module application

Hi,

I'm trying to intercept errors and exceptions following this example: https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Error/

Unfortunately, I fail :(

  1. I'm registering the ErrorHandler in the Module.php of the module I want to intercept errors on.

    class Module implements ModuleDefinitionInterface
    {
    
    public function __construct() {
        ErrorHandler::register(true); // I have stripped the ENV_... stuff out of the plugin and pass a "debug" option here
    }

    The autoloader is set up correctly, the register-method is executed.

Debugging in Handler.php I can see the $dispatcher object is also set up correctly if an error occurs (taken before $dispatcher->dispatch();):

$dispatcher = {Phalcon\Msv\Dispatcher} [17]
    _dependencyInjector = {Phalcon\DI\FactoryDefault} [4]
    _eventsManager = {Phalcon\Events\Manager}
    _activeHandler = {MobileProject\Frontend\Controllers\PublicController} [2]
    _finished = true
    _forwarded = false
    _moduleName = "frontend"
    _namespaceName = "MobileProject\Frontend\Controllers"
    _handlerName = "error"
    _actionName = "phpError"
    _params = {array} [1]
    _returnedValue = null
    _lastHandler = null
    _defaultNamespace = null
    _actionSuffix = "Action"
    _handlerSuffix = "Controller"
    _defaultHandler = "index"
    _defaultAction = "index"

So I want to execute the phpErrorAction in ErrorController - but:

Fatal error: Call to undefined function MobileProject\Frontend\Controllers\scnidsdah() in /var/www/phalcon/apps/frontend/controllers/PublicController.php on line 15
 Fatal error: Uncaught exception 'Phalcon\Mvc\Dispatcher\Exception' with message 'MobileProject\Frontend\Controllers\IndexController handler class cannot be loaded' in /var/www/phalcon/library/Phalcon/Error/Handler.php on line 123
Phalcon\Mvc\Dispatcher\Exception: MobileProject\Frontend\Controllers\IndexController handler class cannot be loaded in /var/www/phalcon/library/Phalcon/Error/Handler.php on line 123

Why is he trying to load the defaultHandler?

Is there any example for a multi module application with error handling?

Thank you, Toby :)



6.4k

Hi, thanks for your reply. Handling the errors is not the problem, I can catch and log them. But if an error occurs, I want to render a special "An error just occured" for the user - so that he sees the complete layout etc. defined for the error page.

That's why I try to set the new controller/action in the dispatcher and make him run again - but it fails. https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Error/Handler.php#L121

Although the controller, namespace, module, action and stuff are set correctly in the dispatcher, he always tries to dispatch the defaults (or doesn't, it's really hard to debug).

I'll set up an example project on github tomorrow.



6.4k

Ok, while setting up the demo project I fixed it. I just didn't see that I, of course, had to use Phalcon\Error\Application instead of Phalcon\Mvc\Application.

If anyone is interested: https://github.com/Plaputta/phalcon-error-test/blob/master/public/index.php#L3



6.4k

And there were 2 more things:

  1. I didn't define the autoloader items that I had already defined in the global autoloader in services.php again in Module.php - but that is needed, it seems.
  2. The Annotations Adapter Apc breaks everything! Using Memory it works just fine, using Apc I get the errors from my first post again.


98.9k

@Plaputta, thanks for the contribution



6.4k

I finally found it! I forgot to define the Controllers Namespace in both the index.php and the Module.php of my real-life project. Now Apc also works like a charm.

I finally found it! I forgot to define the Controllers Namespace in both the index.php and the Module.php of my real-life project. Now Apc also works like a charm.

Hi, can you give me a little help putting this working?