Hi,

Firstly I hope this is the right list for Zend Expressive questions.
Apologies if it isn't.

I've been looking into effective techniques for handling exceptions and
errors in Expressive. This is in the context of an API where I want to
return HTTP error codes to the client when something goes wrong, and also
perform various logging in the background. Pretty standard stuff.

To do this I've experimented with invoking post-route middleware from
within each catch block, but there doesn't seem to be a simple way to pass
the exception object from the catch block to the middleware. I've had
better results by extending the Zend\Stratigility\FinalHandler and invoking
my version from the catch block instead. This works well because the
exception can be passed to the handler via $next().

I was just wondering why the existing Zend\Stratigility\FinalHandler has so
many of its methods declared private though? It seems to me that extending
its behaviour is a valid use case and works well as a means to customise
error handling. But looking at the method signatures you wouldn't think it
was encouraged.

Is there a better/recommended way to handle errors with Expressive?

Incidentally, I have created a 'crash handler' service which deals with
uncaught errors and exceptions, and my custom finalhandler also hands
anything it catches over to the same service. This seems to work really
well but I'm open to adopting a different approach if need be.

Thanks,
- James

Reply via email to