amphp / http-server

An advanced async HTTP server library for PHP, perfect for real-time apps and APIs with high concurrency demands.
https://amphp.org/http-server
MIT License
1.29k stars 101 forks source link

Add ExceptionHandler interface and middleware #375

Closed trowski closed 3 days ago

trowski commented 2 weeks ago

This adds a new interface ExceptionHandler discussed in #374 which is designed to handle uncaught exceptions from a RequestHandler as part of the middleware stack using ExceptionHandlerMiddleware. The interface specifies a single method which takes the uncaught exception and associated Request, returning a Response.

There is a default implementation provided by DefaultExceptionHandler, which logs the uncaught exception to the LoggerInterface instance provided, then returns a response generated by a delegate ErrorHandler.

I've added a parameter to the static constructors of SocketHttpServer for better discovery of the interface, and will add docs to the readme if merged.