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

Exception handling micro framework

Hello everybody,

I'm working with phalcon since a week and I really like how easy it's to understand and the good documentation.

At the moment I'm implmeneting a little api with the micro framework and have here a question about the exception handling.

Is it possible to register an exception handler in the micro framework like mentioned in the dispatching documentation? (https://docs.phalcon.io/en/latest/reference/dispatching.html#handling-not-found-exceptions)

As mentioned in the documentation about the micro framework events (https://docs.phalcon.io/en/latest/reference/micro.html#micro-application-events) a handling of exceptions seems not to be possible or did I miss something?

Best sckoop

edited Sep '15

I just came accross this while trying to implement a generic exception handler for a micro application. That link doesn't seem to apply to micro apps. I am NOT trying to handle not found exceptions; I am trying to set up a handler to catch any uncaught exceptions and, at the very least, set the status code to 500.

use Phalcon\Events\Manager as EvtMan;

$app = new Micro(); $app->setEventsManager(new EvtMan());

$app->get('/api/something', function() { throw new \Exception('JUNK'); });

$app->getEventsManager()->attach('dispatch:beforeException', function($event, $dispatcher, $exception) { die ('fart'); });

A request to /api/something results in an uncaught 'JUNK' exception, I would expect to see 'fart' - what am I missing?

@OakBehringer: This is a solution to your problem. Handling exceptions in Micro is done via 'error' handler.

https://forum.phalcon.io/discussion/11363/micro-error-callable-handler-how-to-get-thrown-exception