On 04/13/2017 02:59 PM, Stefano Torresi wrote:
I initially liked this idea, but on a second thought I think this
could cause some major design issues.
First and foremost, http errors are not exactly exceptional. Every web
application produces negative responses, they're part of their normal
life cycle, so it could be said that this interface encourages using
an exception to control some flow that is actually part of how the
application is expected to work.
Secondarily, middlewares don't need another interface to model the
errors they produce, they can already use the responses, which have
more than enough to provide context to the outer layers: code, reason
phrase and body.
Since one of the points of a middleware collection is to decouple the
stages of the request processing from each other, I think that
offloading this logic to a specific middleware with such a specific
mechanic is an implementation detail.
Now, many could have no problem with it per se, but I don't think
standardising it would be wise.
For example, the double catch block could completely avoided:
class ErrorHandler implements MiddlewareInterface
{
public function process(ServerRequestInterface $request,
DelegateInterface $delegate)
{
try {
$response = $delegate->process($request);
} catch (Exception $e) {
//Generic error 500
$response = $this->factory->createResponse(500);
$this->logger->error('Unexpected error', compact($request, $response,
$e));
return $response;
}
if ($this->debug and is_error($response)) {
$this->logger->debug('Http Error', compact($request, $response));
}
return $response;
}
}
This way the error handler would be catching only truly unexpected
errors, the traditional 500s that make us freak out.
You certainly don't throw exceptions for 404s, do you? ;-)
Symfony-based projects do, currently. That's how HttpKernel works.
I am of very mixed mind about that model myself. It's very easy and
effective for a great many use cases and still forces all output through
a common pathway (good), but as you point out it is arguably using
exceptions for flow of control in non-exceptional situations (bad).
I can see the argument for standardizing HTTP exceptions a la Symfony,
but given that it's a controversial approach (this thread is not the
first time I've seen it criticized, and as I said I have very mixed
emotions about it myself) I am not convinced FIG should come down firmly
on endorsing that approach (which is what such a PSR would do). So I'd
lean toward a pass on this proposal at this point, although could be
convinced otherwise.
--Larry Garfield
--
You received this message because you are subscribed to the Google Groups "PHP
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/php-fig/f301228f-a22f-5932-32cb-e3a497084895%40garfieldtech.com.
For more options, visit https://groups.google.com/d/optout.