This is a design philosophy question, so I know that there is no right answer per se, but I was hoping to get a feeling of the community.
When it comes to handling non-successful requests, there are a few ways of handling what gets returned. The documents seem to be steering developers to throw
ing exceptions which Zend\Stratigility\Middleware\ErrorHandler
can then catch
and process. This seems to be very straight forward, and a nice way to indicate in code that something has gone wrong and to short-circuit the pipeline. I’ve built my program this way and it works for my code very well. I can log at various levels based off the Exception
code with a listener attached to ErrorHandler
, etc.
However, when you look at much of the code in the Expressing and Stratigility libraries, there looks to be more of an emphasis of generating a Response
object with a code instead of throw
ing an exception.
Zend\Expressive\Middleware\RouteMiddleware:L67-70
if ($result->isMethodFailure()) {
return $this->responsePrototype->withStatus(StatusCode::STATUS_METHOD_NOT_ALLOWED)
->withHeader('Allow', implode(',', $result->getAllowedMethods()));
}
I can see a case here where it’s part of the HTTP spec to return the Allowed methods, but this means that not only do I need to handle throw
n exceptions, but need to check the response code to know if I have a good response or not. There is also the case that Zend\Expressive\Application::emitMarshalServerRequestException()
doesn’t check to see if there is a Zend\Stratigility\Middleware\ErrorHandler::class
declared - it skips directly to using the generator, so listeners I’ve attached to ErrorHandler
never get called. @matthew I think you may have addressed this before on some ticket somewhere, but I can’t find it.
What I’m getting at is that there appears to be multiple ways and processes for handling non-successful requests: Throwable
s that will be handled via some sort of ErrorHandler
(per docs and Stratagility classes); Return a Response
object with appropriate code (RouteMiddleware
); Throw the exception directly to an ErrorGenerator
(emitMarshalServerRequestException()
).
Is there a way to perhaps merge these approaches, or is his a case where the possible scenarios are so vast and different, a polyglot approach should be kept?
–MDG