public function onKernelException(GetResponseForExceptionEvent $event) { $e = $event->getException(); $statusCode = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500; // allow 500 errors to be thrown if ($this->debug && $statusCode >= 500) { return; } if ($e instanceof ApiProblemException) { $apiProblem = $e->getApiProblem(); } else { $apiProblem = new ApiProblem($statusCode); /* * If it's an HttpException message (e.g. for 404, 403), * we'll say as a rule that the exception message is safe * for the client. Otherwise, it could be some sensitive * low-level exception, which should *not* be exposed */ if ($e instanceof HttpExceptionInterface) { $apiProblem->set('detail', $e->getMessage()); } } $data = $apiProblem->toArray(); $response = new JsonResponse($data, $apiProblem->getStatusCode()); $response->headers->set('Content-Type', 'application/json'); $event->setResponse($response); }
/** * ApiProblemException constructor. * * @param \BiteCodes\RestApiGeneratorBundle\Api\Response\ApiProblem $apiProblem * @param \Exception|null $previous * @param array $headers * @param int $code */ public function __construct(ApiProblem $apiProblem, \Exception $previous = null, array $headers = array(), $code = 0) { $this->apiProblem = $apiProblem; $statusCode = $apiProblem->getStatusCode(); $message = $apiProblem->getTitle(); parent::__construct($statusCode, $message, $previous, $headers, $code); }