I’m trying to catch all the Exceptions on my prefix /api/v1.
I’ve read the book and made this:
config/routes.php
src/Controller/Api/V1/ErrorController.php
My problem is that if I throw an Exception, the controller displays the template in templates/Error/error500.php even if I use $event->stopPropagation().
Instead, I need to return just the number “1”.
I’ve found a messy workaround which is calling echo “1” and die, but I dont want to do this because I’ve found a content length bigger than 1 on the response header in some servers.
What am I doing wrong? Thanks in advance!
[SOLVED] Finally fixed it with this code…
<?php
declare(strict_types=1);
namespace App\Controller\Api\V1;
use Cake\Controller\Controller;
use Cake\Event\EventInterface;
class ErrorController extends Controller
{
public function beforeRender(EventInterface $event)
{
$event->stopPropagation();
$this->response = $this->response
->withHeader('Connection', 'close')
->withLength(1)
->withType('text/html')
->withStringBody('0');
}
}