public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); $attributes = array('_controller' => $this->controller, 'exception' => FlattenException::create($exception), 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, 'format' => $request->getRequestFormat()); $request = $request->duplicate(null, null, $attributes); $request->setMethod('GET'); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false); } catch (\Exception $e) { $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false); $wrapper = $e; while ($prev = $wrapper->getPrevious()) { if ($exception === ($wrapper = $prev)) { throw $e; } } $prev = new \ReflectionProperty('Exception', 'previous'); $prev->setAccessible(true); $prev->setValue($wrapper, $exception); throw $e; } $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { if ($event->getException() instanceof AreasNotSupportedException) { $subRequest = $this->httpUtils->createRequest($event->getRequest(), 'cantiga_error_areas_not_supported'); $event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true)); } }
public function onKernelException(GetResponseForExceptionEvent $event) { if (!(Ki_ENVIRONMENT == 'prod') || Ki_ENVIRONMENT == 'prod' && Ki_DEBUG === true) { // Si retorna muestra el error detallado. return; } $Exception = $event->getException(); $status = $Exception->getStatusCode(); $message = $Exception->getMessage(); /** * @var $view ViewBuilder */ $view = Service::get('view'); /** * @var $request Request */ $request = Service::get('new.request'); $request->setMethod('GET'); $request = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false); if (in_array($status, array(404))) { $content = $view->render('exceptions/error404', array('exception_message' => $message)); } else { if (in_array($status, array(401, 403))) { $content = $view->render('exceptions/error403', array('exception_message' => $message)); } else { if (in_array($status, array(409))) { $content = $view->render('exceptions/error409', array('exception_message' => $message)); } } } $request->setContent($content); // Envia la respuesta $event->setResponse($request); }
public function onKernelException(GetResponseForExceptionEvent $event) { static $handling; if (true === $handling) { return false; } $handling = true; $exception = $event->getException(); $request = $event->getRequest(); $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); $attributes = array('_controller' => $this->controller, 'exception' => FlattenException::create($exception), 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, 'format' => $request->getRequestFormat()); $request = $request->duplicate(null, null, $attributes); $request->setMethod('GET'); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { $this->logException($exception, sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()), false); // set handling to false otherwise it wont be able to handle further more $handling = false; // re-throw the exception from within HttpKernel as this is a catch-all return; } $event->setResponse($response); $handling = false; }
public function onComunidadNoSeleccionadaException(GetResponseForExceptionEvent $event) { $kernel = $event->getKernel(); $exception = $event->getException(); if (!$exception instanceof ComunidadNoSeleccionadaException) { return; } $kernel->handle($event->getRequest(), HttpKernel::SUB_REQUEST); }
public function onKernelException(GetResponseForExceptionEvent $event) { static $handling; if (true === $handling) { return false; } $handling = true; $exception = $event->getException(); $request = $event->getRequest(); $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); $attributes = array( '_controller' => $this->controller, 'exception' => FlattenException::create($exception), 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, // keep for BC -- as $format can be an argument of the controller callable // see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php // @deprecated in 2.4, to be removed in 3.0 'format' => $request->getRequestFormat(), ); $request = $request->duplicate(null, null, $attributes); $request->setMethod('GET'); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false); // set handling to false otherwise it wont be able to handle further more $handling = false; $wrapper = $e; while ($prev = $wrapper->getPrevious()) { if ($exception === $wrapper = $prev) { throw $e; } } $prev = new \ReflectionProperty('Exception', 'previous'); $prev->setAccessible(true); $prev->setValue($wrapper, $exception); throw $e; } $event->setResponse($response); $handling = false; }
public function onException(GetResponseForExceptionEvent $event) { $request = Request::create($this->error_path); $request->attributes->add(array('exception' => FlattenException::create($event->getException()))); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { return; } $event->setResponse($response); }
/** * @param GetResponseForExceptionEvent $event */ public function onExceptionThrown(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $kernel = $event->getKernel(); // Load errorCode Controller if exist else throw exception again if ($exception instanceof NotFoundHttpException) { $response = $kernel->handle(Request::create('_/Error/code404'), HttpKernelInterface::MASTER_REQUEST, false); } else { $response = $kernel->handle(Request::create('_/Error/code500'), HttpKernelInterface::MASTER_REQUEST, false); } $event->setResponse($response); }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $statusCode = $this->getHttpStatusCode($exception->getCode()); if ($this->application->isDebugMode()) { $this->response = $this->getDebugTraceResponse($exception, $statusCode); } else { $this->response = $this->getErrorPageResponse($exception, $statusCode); } $event->setResponse($this->response); $filterEvent = new FilterResponseEvent($event->getKernel(), $event->getRequest(), $event->getRequestType(), $event->getResponse()); $event->getDispatcher()->dispatch(KernelEvents::RESPONSE, $filterEvent); }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!($exception = $event->getException()) instanceof ApiException) { return; } $request = $event->getRequest(); $attributes = array('_controller' => $this->controller, 'exception' => $exception, 'format' => $request->getRequestFormat('json')); $request = $request->duplicate(null, null, $attributes); $request->setMethod('GET'); $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); $event->setResponse($response); $event->stopPropagation(); }
public function onKernelException(GetResponseForExceptionEvent $event) { /** @var HttpExceptionInterface $exception */ $exception = $event->getException(); if ($exception instanceof HttpExceptionInterface) { $request = clone $event->getRequest(); $request->attributes->set('_controller', 'UCCA\\Controllers\\HttpExceptionController'); $request->attributes->set('status_code', $exception->getStatusCode()); $request->attributes->remove('action'); $request->query->remove('action'); $request->request->remove('action'); $event->setResponse($event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false)); } }
public function onKernelException(GetResponseForExceptionEvent $event) { static $handling; if (true === $handling) { return false; } $handling = true; $exception = $event->getException(); $request = $event->getRequest(); if (null !== $this->logger) { $message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()); if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) { $this->logger->crit($message); } else { $this->logger->err($message); } } else { error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); } $logger = $this->logger instanceof DebugLoggerInterface ? $this->logger : null; $flattenException = FlattenException::create($exception); if ($exception instanceof HttpExceptionInterface) { $flattenException->setStatusCode($exception->getStatusCode()); $flattenException->setHeaders($exception->getHeaders()); } $attributes = array('_controller' => $this->controller, 'exception' => $flattenException, 'logger' => $logger, 'format' => $request->getRequestFormat()); $request = $request->duplicate(null, null, $attributes); $request->setMethod('GET'); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { $message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()); if (null !== $this->logger) { if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) { $this->logger->crit($message); } else { $this->logger->err($message); } } else { error_log($message); } // set handling to false otherwise it wont be able to handle further more $handling = false; // re-throw the exception as this is a catch-all throw $exception; } $event->setResponse($response); $handling = false; }
/** * Render a control panel error page for the exception. * * @param GetResponseForExceptonEvent $event The event object */ public function renderErrorPage(GetResponseForExceptionEvent $event) { // Skip if working locally if ('local' === $this->get('env')) { return false; } // Skip if the requested route is not in the control panel if (!is_array($event->getRequest()->get('_route_collections')) || !in_array('ms.cp', $event->getRequest()->get('_route_collections'))) { return false; } $request = $event->getRequest()->duplicate(null, null, array('_controller' => 'Message\\Mothership\\ControlPanel\\Controller\\Error::exception', '_format' => $event->getRequest()->getRequestFormat(), 'exception' => $event->getException())); $request->setMethod('GET'); $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $exceptionClass = get_class($exception); if (isset($this->redirections[$exceptionClass]) && !empty($this->redirections[$exceptionClass])) { switch (true) { case $exception instanceof PermissionRequiredException: break; default: return; } $kernel = $event->getKernel(); $request = $event->getRequest()->duplicate(null, null, ['_controller' => $this->redirections[$exceptionClass], 'exception' => $exception]); $response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false); $event->setResponse($response); } }
/** * Get correct response for validation failed exception * * @param GetResponseForExceptionEvent $event event * @param EventDispatcherInterface $eventDispatcher eventDispatcher * @param ValidationFailedException $exception exception * * @return Response|null */ private function getValidationFailedExceptionResponse(GetResponseForExceptionEvent $event, EventDispatcherInterface $eventDispatcher, ValidationFailedException $exception) { $event->stopPropagation(); $request = $event->getRequest(); $transaction = $request->get('transaction'); if (!$transaction) { return $event->getResponse(); } $data = $request->get($request->get(Alias::DATA)); $violations = $exception->getConstraintViolationList(); $request->attributes->set('violations', $violations); $view = View::create($data); $responseEvent = new GetResponseForControllerResultEvent($event->getKernel(), $request, $request->getMethod(), $view); if ($view->getData() instanceof ArrayCollection) { $responseEvent->getRequest()->attributes->set(EmbeddedManager::KEY_EMBED, EmbeddedManager::GROUP_VIOLATION_COLLECTION); } else { $responseEvent->getRequest()->attributes->set(EmbeddedManager::KEY_EMBED, EmbeddedManager::GROUP_VIOLATION_ENTITY); } $eventDispatcher->dispatch('kernel.view', $responseEvent); return $responseEvent->getResponse(); }
/** * Handle exceptions that occur during the request handling process. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * * @throws \Exception|\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException|\Symfony\Component\HttpKernel\Exception\HttpException * As a fallback behaviour only, if a sensible error page cannot be generated. */ public function onKernelException(GetResponseForExceptionEvent $event) { LoggerRegistry::debug('EngineExceptionListener performing handleException() on EXCEPTION event'); $exception = $event->getException(); try { // Delete any buffered page content. while (ob_get_level() > 1) { ob_end_clean(); } // Determine the status code to use for the error page (and which error page is displayed). $statusCode = $exception instanceof HttpException ? $exception->getStatusCode() : ($exception instanceof FileNotFoundException ? 404 : 500); // Set route and template attributes to error page defaults. $event->getRequest()->attributes->add(array('_status' => $statusCode, '_route' => $this->getEngine()->getErrorRoute($statusCode), '_route_params' => array('exception' => $exception, 'statusCode' => $statusCode), '_template' => $this->getEngine()->getErrorTemplate())); // Store the exception and status code in the top-level view data. $page = $this->getEngine()->getViewFactory()->getPage(); $page['exception'] = $exception; $page['status-code'] = $statusCode; // Perform standard rendering using a proxy event. $renderer = new EngineRendererListener($this->getEngine()); $viewScript = sprintf('error-%3d', $statusCode); // TODO Handle view scripts that don't exist, default back to 500 or 404 $renderEvent = new GetResponseForControllerResultEvent($event->getKernel(), $event->getRequest(), HttpKernelInterface::SUB_REQUEST, $viewScript); $renderer->onKernelView($renderEvent); // Copy the response from the proxy event back to the event being handled. $event->setResponse($renderEvent->getResponse()); } catch (\Exception $e) { try { // Delete any buffered page content. while (ob_get_level() > 1) { ob_end_clean(); } // If there is an error above, try to display the fallback error page, i.e. black-and-white error // message. $event->setResponse(Response::create(HtmlUtilities::exception($exception, $this->getEngine()->getSiteInfo()->getAdministratorName(), $this->getEngine()->getSiteInfo()->getAdministratorEmail()))); } catch (\Exception $e2) { // If another error occurs, the best thing to do is throw the original error. throw $exception; } } }
public function onKernelException(GetResponseForExceptionEvent $event) { static $handling; if (true === $handling) { return false; } $handling = true; $exception = $event->getException(); $request = $event->getRequest(); $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); $request = $this->duplicateRequest($exception, $request); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false); // set handling to false otherwise it wont be able to handle further more $handling = false; // throwing $e, not $exception, is on purpose: fixing error handling code paths is the most important throw $e; } $event->setResponse($response); $handling = false; }
public function onKernelException(GetResponseForExceptionEvent $event) { static $handling; if (true === $handling) { return false; } $handling = true; $exception = $event->getException(); $request = $event->getRequest(); $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); $request = $this->duplicateRequest($exception, $request); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { $this->logException($exception, sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()), false); // set handling to false otherwise it wont be able to handle further more $handling = false; // re-throw the exception from within HttpKernel as this is a catch-all return; } $event->setResponse($response); $handling = false; }
/** * Get correct response for validation failed exception * * @param GetResponseForExceptionEvent $event event * @param EventDispatcherInterface $eventDispatcher eventDispatcher * @param ValidationFailedException $exception exception * * @return Response|null */ private function getValidationFailedExceptionResponse(GetResponseForExceptionEvent $event, EventDispatcherInterface $eventDispatcher, ValidationFailedException $exception) { $event->stopPropagation(); $request = $event->getRequest(); $transaction = $request->get('transaction'); if (!$transaction) { return $event->getResponse(); } $data = $request->get($request->get(Alias::DATA)); $violations = $exception->getConstraintViolationList(); $request->attributes->set('violations', $violations); $view = View::create($data); $responseEvent = new GetResponseForControllerResultEvent($event->getKernel(), $request, $request->getMethod(), $view); $eventDispatcher->dispatch('kernel.view', $responseEvent); $responseData = $view->getData(); if ($responseData instanceof EmbeddedInterface) { $responseData->setShowAssociations(true); } if ($responseData instanceof CollectionResponse) { $responseData->setInheritedShowAssociations(false); } return $responseEvent->getResponse(); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); $request = $this->duplicateRequest($exception, $request); try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false); } catch (\Exception $e) { $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false); $wrapper = $e; while ($prev = $wrapper->getPrevious()) { if ($exception === ($wrapper = $prev)) { throw $e; } } $prev = new \ReflectionProperty('Exception', 'previous'); $prev->setAccessible(true); $prev->setValue($wrapper, $exception); throw $e; } $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $statusCode = $exception->getCode(); if ($this->application->isDebugMode()) { $this->response = (new \Symfony\Component\Debug\ExceptionHandler())->createResponse($exception); } if (!$this->application->isDebugMode()) { switch ($statusCode) { case 404: case 500: $parameterKey = "error.{$statusCode}"; break; default: $parameterKey = 'error.default'; } $parameter = $this->application->getContainer()->getParameter($parameterKey); $view = $this->getErrorTemplate($parameter); $this->response->setStatusCode($statusCode)->setContent($this->renderer->partial($view, ['error' => $exception])); } $event->setResponse($this->response); $filterEvent = new FilterResponseEvent($event->getKernel(), $event->getRequest(), $event->getRequestType(), $event->getResponse()); $event->getDispatcher()->dispatch(KernelEvents::RESPONSE, $filterEvent); }
private function handleAccessDeniedException(GetResponseForExceptionEvent $event, AccessDeniedException $exception) { $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception)); $token = $this->context->getToken(); if (!$this->authenticationTrustResolver->isFullFledged($token)) { if (null !== $this->logger) { $this->logger->debug(sprintf('Access is denied (user is not fully authenticated) by "%s" at line %s; redirecting to authentication entry point', $exception->getFile(), $exception->getLine())); } try { $insufficientAuthenticationException = new InsufficientAuthenticationException('Full authentication is required to access this resource.', 0, $exception); $insufficientAuthenticationException->setToken($token); $event->setResponse($this->startAuthentication($event->getRequest(), $insufficientAuthenticationException)); } catch (\Exception $e) { $event->setException($e); } return; } if (null !== $this->logger) { $this->logger->debug(sprintf('Access is denied (and user is neither anonymous, nor remember-me) by "%s" at line %s', $exception->getFile(), $exception->getLine())); } try { if (null !== $this->accessDeniedHandler) { $response = $this->accessDeniedHandler->handle($event->getRequest(), $exception); if ($response instanceof Response) { $event->setResponse($response); } } elseif (null !== $this->errorPage) { $subRequest = $this->httpUtils->createRequest($event->getRequest(), $this->errorPage); $subRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $exception); $event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true)); } } catch (\Exception $e) { if (null !== $this->logger) { $this->logger->error(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage())); } $event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e)); } }
/** * Handles security related exceptions. * * @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); // determine the actual cause for the exception while (null !== ($previous = $exception->getPrevious())) { $exception = $previous; } if ($exception instanceof AuthenticationException) { if (null !== $this->logger) { $this->logger->info(sprintf('Authentication exception occurred; redirecting to authentication entry point (%s)', $exception->getMessage())); } try { $response = $this->startAuthentication($request, $exception); } catch (\Exception $e) { $event->setException($e); return; } } elseif ($exception instanceof AccessDeniedException) { $token = $this->context->getToken(); if (!$this->authenticationTrustResolver->isFullFledged($token)) { if (null !== $this->logger) { $this->logger->debug('Access denied (user is not fully authenticated); redirecting to authentication entry point'); } try { $response = $this->startAuthentication($request, new InsufficientAuthenticationException('Full authentication is required to access this resource.', $token, 0, $exception)); } catch (\Exception $e) { $event->setException($e); return; } } else { if (null !== $this->logger) { $this->logger->debug('Access is denied (and user is neither anonymous, nor remember-me)'); } try { if (null !== $this->accessDeniedHandler) { $response = $this->accessDeniedHandler->handle($request, $exception); if (!$response instanceof Response) { return; } } elseif (null !== $this->errorPage) { $subRequest = $this->httpUtils->createRequest($request, $this->errorPage); $subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception); $response = $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); $response->setStatusCode(403); } else { $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception)); return; } } catch (\Exception $e) { if (null !== $this->logger) { $this->logger->err(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage())); } $event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e)); return; } } } elseif ($exception instanceof LogoutException) { if (null !== $this->logger) { $this->logger->info(sprintf('Logout exception occurred; wrapping with AccessDeniedHttpException (%s)', $exception->getMessage())); } $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception)); return; } else { return; } $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $kernel = $event->getKernel(); if (!$exception instanceof ControllerNotAvailableException) { error_log($exception->getMessage()); return; } $attributes = array('_controller' => $this->getOnErrorRedirectTo(), 'code' => $exception->getCode()); $request = $event->getRequest()->duplicate(null, null, $attributes); $response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false); $event->setResponse($response); }
public function onSilexError(GetResponseForExceptionEvent $event) { $app = $event->getKernel(); $handler = new DebugExceptionHandler($app['debug']); $event->setResponse($handler->createResponse($event->getException())); }