I have a custom ExceptionHandler, NotFoundHandler and NotAllowedHandler.
I am using Gofabian Negotiation Middleware
If I access a missing page then the request sent to NotFoundHandler::__invoke contains the middleware.
If I throw an Exception deep within my app I would expect it to contain the same middleware but it does not.
I’m assigning the ExceptionHandler like so:
// handle missing pages
$container['notFoundHandler'] = function($c) {
return new \My\Exceptions\NotFoundHandler($c);
};
// handle access issues
$container['notAllowedHandler'] = function($c) {
return new \My\Exceptions\NotAllowedHandler($c);
};
// handle PHP7 exceptions
$container['phpErrorHandler'] = function($c) {
return new \My\Exceptions\ExceptionHandler($c);
};
// handle all other exceptions
$container['errorHandler'] = function($c) {
return new \My\Exceptions\ExceptionHandler($c);
};
My Handlers contain this line in the __invoke method:
public function __invoke(Request $request, Response $response, $exception)
{
switch ($request->getAttribute('negotiation')->getMediaType())
{
// logic here
}
Which in the case of the NotFound and NotAllowed handlers works fine, but in the ExceptionHandler case it throws another exception because the middleware is not set and I’m calling the getMediaType() method on null.
My question seems to be similar to this one Manually thrown exceptions do not traverse the middleware stack It seems to be advocating littering my code with try/ctach in order to stay clean… that’s not what I want.
For specific exceptions I will use try/catch but for everything else I want it to be handled by the default handler.
How can I get the processed middleware $request into my ExceptionHandler?