k911 / swoole-bundle

Symfony Swoole Bundle
MIT License
260 stars 46 forks source link

Some error's do not log. #568

Open maxkain opened 2 years ago

maxkain commented 2 years ago

Maybe it is Symfony issue, but it works fine with php-fpm or APP_DEV for unknown reasons.

Some errors are not hanled, messages disappear, for example php type error of function argument or using null as object. I fixed it by decorating Symfony HttpKernel, it catches only \Exception, but not \Throwable:

services.yaml

    App\Service\HttpKernel\HttpKernel:
        parent: http_kernel
        decorates: http_kernel

App\Service\HttpKernel\HttpKernel

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel as BaseHttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class HttpKernel extends BaseHttpKernel
{
    public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true)
    {
        try {
            return parent::handle($request, $type, $catch);
        } catch (\Throwable $e) {
            $ref = (new \ReflectionObject($this))->getParentClass();

            if (false === $catch) {
                $finishRequest = $ref->getMethod('finishRequest');
                $finishRequest->setAccessible(true);

                $finishRequest->invoke($this, $request, $type);

                throw $e;
            }

            $handleThrowable = $ref->getMethod('handleThrowable');
            $handleThrowable->setAccessible(true);

            return $handleThrowable->invoke($this, $e, $request, $type);
        }
    }
}