getThrowable(); if ($e instanceof ApiErrorException) { $apiErrorMsg = $e->getApiErrorMsg(); } else { $statusCode = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500; $apiErrorMsg = new ApiErrorMessage($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) { //Check if it's a Validation error. // When using MapRequestPayload() ValidationErrors throw HttpExceptions. $prev = $e->getPrevious(); if ($prev instanceof ValidationFailedException) { //Get validation error details. $errorList = []; $violationList = $prev->getViolations(); foreach ($violationList as $violation) { $errorList[] = [ 'field' => $violation->getPropertyPath(), 'message' => $violation->getMessage(), ]; } $apiErrorMsg->setExtraData('detail', "Validation Error. See errorList for details"); $apiErrorMsg->setExtraData('errorList', $errorList); } else { //Get exception error message. $apiErrorMsg->setExtraData('detail', $e->getMessage()); } } } //Send error to logs. $exceptionError = [ 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTrace() ]; $errorContext = array_merge($apiErrorMsg->toArray(), $exceptionError); $this->logger->error("API ERROR Exception:", $errorContext); //Send error to client. $response = new JsonResponse( $apiErrorMsg->toArray(), $apiErrorMsg->getStatusCode() ); $response->headers->set('Content-Type', 'application/problem+json'); $event->setResponse($response); } public static function getSubscribedEvents(): array { return [ KernelEvents::EXCEPTION => 'onKernelException', ]; } }