/** * Map an exception to an error screen. * * @param GetResponseForExceptionEvent $event The event object */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest() || 'html' !== $event->getRequest()->getRequestFormat()) { return; } $this->handleException($event); }
/** * Handles the onKernelException event. * * @param GetResponseForExceptionEvent $event A GetResponseForExceptionEvent instance */ public function onKernelException(GetResponseForExceptionEvent $event) { if ($this->onlyMasterRequests && !$event->isMasterRequest()) { return; } $this->exception = $event->getException(); }
/** * @param GetResponseForExceptionEvent $event */ public function onException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest()) { return; } $this->eventDispatcher->dispatch(Events::REQUEST_ENDS, new RequestEnded($event->getRequest(), $event->getResponse(), $event->getException())); }
/** * Log de l'erreur dans MongoDB */ public function onKernelException(GetResponseForExceptionEvent $event) { // don't do anything if it's not the master request if (!$event->isMasterRequest()) { return; } // Create logs try { $log = new ErrorLog(); $exception = $event->getException(); $request = $event->getRequest(); // Current User if ($this->securityContext->getToken() && $this->securityContext->isGranted('ROLE_USER')) { $log->setUsername($this->securityContext->getToken()->getUser()->getUsername()); $log->setUserid($this->securityContext->getToken()->getUser()->getId()); } else { $log->setUsername(null); } // Get error code if (method_exists($exception, 'getStatusCode')) { $log->setCode($exception->getStatusCode()); } else { $log->setCode($exception->getCode()); } $log->setMessage($exception->getMessage()); $log->setUrl($request->getUri()); $log->setIp($request->getClientIp()); $log->setReferer($request->headers->get('referer')); if (in_array($log->getCode(), array('0', '404', '500'))) { $this->em->persist($log); $this->em->flush(); } } catch (\Exception $e) { } }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest()) { return; } $request = $event->getRequest(); $exception = $event->getException(); if ($exception instanceof HttpException) { $status = $exception->getStatusCode(); $headers = $exception->getHeaders(); } elseif ($exception instanceof DeserializationException) { $status = Response::HTTP_BAD_REQUEST; $headers = []; } else { $code = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : $exception->getCode(); $status = isset(Response::$statusTexts[$code]) ? $code : Response::HTTP_BAD_REQUEST; $headers = []; } // Normalize exceptions with hydra errors only for resources if ($request->attributes->has('_resource')) { $dataResponse = $this->serializer->serialize($exception, $this->format); $response = new Response($dataResponse, $status, $headers); if ($this->format === 'xml') { $response = new XmlResponse($dataResponse, $status, $headers); } $event->setResponse($response); } }
public function onKernelException(GetResponseForExceptionEvent $event) { $this->logger->notice(sprintf('Exceptions catcher listener: catch kernel.exception event (exception: %s)', $event->getException()->getMessage())); // If this is not a master request, skip handling if (!$event->isMasterRequest()) { $this->logger->debug('Exceptions catcher listener: this is not master request, skip'); return; } // If content already prepared if ($event->hasResponse()) { $this->logger->debug('Exceptions catcher listener: event already has response, skip'); return; } // Getting action $apiServerAction = $event->getRequest()->attributes->get('apiAction'); /* @var $apiServerAction ApiServerAction */ // Something wrong if (!$apiServerAction) { $this->logger->error('Request parser listener: request has no apiAction attribute, sending empty response'); $event->setResponse(new JsonResponse([])); return; } // Getting api server interface $apiServerInterface = $apiServerAction->getApiServerInterface(); // Creating api response $apiResponse = $apiServerInterface->getExceptionResponse($event->getException()->getMessage()); // Setting response $event->setResponse(new JsonResponse($apiResponse->export())); }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest()) { return; } $request = $event->getRequest(); if (!$request->attributes->has('_resource_type') || self::FORMAT !== $request->attributes->get('_api_format')) { return; } $exception = $event->getException(); $headers = []; if ($exception instanceof HttpException) { $status = $exception->getStatusCode(); $headers = $exception->getHeaders(); $data = $exception; } elseif ($exception instanceof ValidationException) { $status = Response::HTTP_BAD_REQUEST; $data = $exception->getConstraintViolationList(); } elseif ($exception instanceof ExceptionInterface || $exception instanceof InvalidArgumentException) { $status = Response::HTTP_BAD_REQUEST; $data = $exception; } else { $status = Response::HTTP_INTERNAL_SERVER_ERROR; $data = $exception; } $event->setResponse(new Response($this->normalizer->normalize($data, 'hydra-error'), $status, $headers)); }
/** * @param GetResponseForExceptionEvent $event */ public function onException(GetResponseForExceptionEvent $event) { if (!$event->getException() instanceof NotFoundHttpException) { return; } if (!$event->isMasterRequest()) { return; } if (!($redirect = $this->getRedirectForRequest($event->getRequest()))) { return; } $event->setResponse($redirect); }
/** * Kernel exception listener callback. * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest()) { return; } $app = $this->app; if (!$app->isTestMode()) { if ($app['orm.em']->getConnection()->isTransactionActive()) { $app['orm.em']->rollback(); } } else { $this->app->log('TestCase to onKernelException of rollback'); } }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest()) { return; } $request = $event->getRequest(); $exception = $event->getException(); if ($exception instanceof HttpException) { $status = $exception->getStatusCode(); $headers = $exception->getHeaders(); } elseif ($exception instanceof DeserializationException) { $status = Response::HTTP_BAD_REQUEST; $headers = []; } else { $status = Response::HTTP_INTERNAL_SERVER_ERROR; $headers = []; } // Normalize exceptions with hydra errors only for resources if ($request->attributes->has('_resource')) { $event->setResponse(new Response($this->normalizer->normalize($exception, 'hydra-error'), $status, $headers)); } }
/** * * @author Krzysztof Bednarczyk * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->isMasterRequest()) { return; } $exception = $event->getException(); $isXVRequest = (int) $event->getRequest()->headers->get('X-XV-Request', 0); if ($exception instanceof AccessDeniedHttpException || $exception instanceof AccessDeniedHttpException || $exception instanceof AuthenticationException) { $path = parse_url($event->getRequest()->getRequestUri(), PHP_URL_PATH); $event->setResponse(new RedirectResponse("/login/?r={$path}")); $event->stopPropagation(); return; } if ($exception instanceof NotFoundHttpException) { $event->setResponse(new RedirectResponse("/page/404/" . ($isXVRequest ? "?dialog=true" : ""))); $event->stopPropagation(); return; } if ($exception instanceof CsrfHttpException) { $event->setResponse(new RedirectResponse("/page/csrf/")); $event->stopPropagation(); return; } }