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

Using a class exception handler in Micro Application

Hi all,

I am wondering if there's a way to do something like

$app->error([new \exceptions\ExceptionHandler(), 'handle']);

instead of

$app->error( function ($exception) { echo "An error has occurred"; } );

I would rather use a class than a closure, is it documented somewhere how can I use this in a Micro application?



627

@Ahmed Sabaa

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/micro.zep#L454 : here we can clearly see that param is just callable

https://php.net/manual/ru/language.types.callable.php describes what is callable, particularly

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1

I don't think you can. But what you can do is instantiate your object inside this error handler. So you can re-use catched Exception from the closure itself.



627

I don't think you can. But what you can do is instantiate your object inside this error handler. So you can re-use catched Exception from the closure itself.

please read my comment carefully. it explains (with a references to a documentation) why it can and how to do

Well it should work without problem as an array. Isn't it workin for you ? Did you have any error ?



627

Well it should work without problem as an array. Isn't it workin for you ? Did you have any error ?

Are you drunk?? Just out of curioucity...

edited May '16

What ? He just asked a question if something like this is possible. Then yes - it's possible, his code should work, so im just asking simple question. What's not working ?

thanks for the answers, but obviously if I have the code snippet ready and already in the wquestion, that means that I already tried it and it didn't work. the idea behind my question is if I am doing something wrong or if it's not supported at all.

I have tried this and I didn't get any errors or anything, but when an exception is thrown, it goes "uncaught" and just stops the request and returns a 500 error

edited May '16

Ahmed Sabaa,

Here's a tested / working example:

class mijnErrorHandler{
    function err($err){
        var_dump($err);
    }
}

//Here's definition of error handler in Micro then:

$app->error([new mijnErrorHandler(), 'err']);

Once your app throws an exception, it will be routed through this micro error handler, which will call your custom class to handle it. If you got 'Error handler is not callable' message, it is clear that you need to set your method as public.

edited May '16

I don't think you can. But what you can do is instantiate your object inside this error handler. So you can re-use catched Exception from the closure itself.

please read my comment carefully. it explains (with a references to a documentation) why it can and how to do

Yes, I just tested it - it's somewhat workaround rather than a documented feature. For my Micro app (API), default Phalcon error handler is enough as it accepts thrown exceptions as first argument of a closure.

And you don't have to be that rude without any reason. @jurigag is trying to help with any issue here on the Phorum. We're all here to learn and help others.

edited May '16

It's not like workaround, just a normal feature built-in php that you can pass array to call_user_func_array

But it should working without any problem. Also you sure that you handled this error(returned response or false ?) https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/micro.zep#L857

He is just telling that exception is throw anyways i guess, so he just did something in his code, but forgot about returning false and that's why exception is still thrown.