Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



114.3k
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
});