Welcome Guest, Not a member yet? Register   Sign In
Exception to interrupt script execution. (another option for custom exceptions.)
#1

The framework catches only two exceptions - HTTP 404 and redirect. It seems to me that it is possible to optimize the 'catch' block by adding an interface, which will expand the possibilities for using exceptions as interruption of request processing.

Now catching exceptions looks like this.

PHP Code:
try {
    return $this->handleRequest($routes$cacheConfig$returnResponse);
} catch (
RedirectException $e) {
} catch (
PageNotFoundException $e) {


That is, to catch another exception, you need to add another catch block.

An interface like this could be the solution.

PHP Code:
interface HttpExceptionInterface
{
    public function response(RequestInterface $request): ResponseInterface;


And then catching exceptions will look like this.

PHP Code:
try {
    return $this->handleRequest($routes$cacheConfig$returnResponse);
} catch (
HttpExceptionInterface $e) {
    $response $e->response($this->request);
    // do something with response

That is, the response generation logic will be moved to the exception class.

By implementing such an interface, an exception can be thrown in filters, controller constructor, models and services.
Reply
#2

Exceptions are not responses.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB