<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Throwable;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
{
try {
return parent::handle($request, $type, false);
} catch (Throwable $exception) {
$statusCode = method_exists($exception, 'getStatusCode')
? $exception->getStatusCode()
: 500;
$response = [
'message' => $this->getEnvironment() === 'dev'
? $exception->getMessage()
: 'An error occurred',
'code' => $statusCode
];
if ($this->getEnvironment() === 'dev') {
$response['error']['file'] = $exception->getFile();
$response['error']['line'] = $exception->getLine();
$response['error']['trace'] = $exception->getTraceAsString();
}
return new JsonResponse($response, $statusCode);
}
}
}