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

Micro error (callable $handler) - how to get thrown exception?

public Phalcon\Mvc\Micro error (callable $handler)

Sets a handler that will be called when an exception is thrown handling the route

In Micro app, is there a way to read (getMessage) thrown exception inside this error handler?

I believe Phalcon\Mvc\Micro\Exception should be read somehow. I.e. that would be getMessage() method.



145.0k
Accepted
answer

The first argument passed to handler will be exception.

Wojciech, thanks. So first argument of the error's closure. Gonna give it a try, I'm offsite now

edited May '16

Hello, I have tested it. It works. :) Thanks @Jurigag !

The good thing is to have multiple error handlers for different exception types:

$app->error(function (\PDOException $e = null) use ($app) {
//catch RDBMS exceptions
});

$app->error(function (\Phalcon\Mvc\Micro\Exception $e = null) use ($app) {
//catch only Phalcon (Micro) exceptions here
});

Naah, it seems that you cannot have more than one error handler. It will always trigger the last defined, i.e. it will not check type cast like in my example. So, one global handler of type Exception is what it is:

$app->error(function (\Exception $e = null) use ($app) {
//catch all exceptions here
});

Hello, I have tested it. It works. :) Thanks @Jurigag steamdb!

The good thing is to have multiple error handlers for different exception types:

$app->error(function (\PDOException $e = null) use ($app) {
//catch RDBMS exceptions
});

$app->error(function (\Phalcon\Mvc\Micro\Exception $e = null) use ($app) {
//catch only Phalcon (Micro) exceptions here
});

I don't think micro supports annotations.