/** * Renders the defined {@see ExceptionListener::$errorTemplate}, which has been defined via YAML * settings, on exception. * * Note, that the function is only called, if the *debug value* is set or *error pages* are * enabled via Parameter *stvd.error_page.enabled*. * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { // don't do anything if it's not the master request if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) { return; } // You get the exception object from the received event $exception = $event->getException(); // Customize your response object to display the exception details $response = new Response(); // set response content $response->setContent($this->templating->render($this->errorTemplate, array('exception' => $exception))); // HttpExceptionInterface is a special type of exception that // holds status code and header details if ($exception instanceof HttpExceptionInterface) { $response->setStatusCode($exception->getStatusCode()); $response->headers->replace($exception->getHeaders()); } else { // If the exception's status code is not valid, set it to *500*. If it's valid, the // status code will be transferred to the response. if ($exception->getCode()) { $response->setStatusCode($exception->getCode()); } else { $response->setStatusCode(500); } } // Send the modified response object to the event $event->setResponse($response); }
/** * Handles the onCoreException event. * * @param GetResponseForExceptionEvent $event A GetResponseForExceptionEvent instance */ public function onCoreException(GetResponseForExceptionEvent $event) { if ($this->onlyMasterRequests && HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $this->exception = $event->getException(); }
public function onKernelException(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $request = $event->getRequest(); if ('' !== rtrim($request->getPathInfo(), '/')) { return; } $ex = $event->getException(); if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) { return; } $locale = $this->localeResolver->resolveLocale($request, $this->locales) ?: $this->defaultLocale; $request->setLocale($locale); $params = $request->query->all(); unset($params['hl']); $response = new RedirectResponse($request->getBaseUrl() . '/' . $locale . '/' . ($params ? '?' . http_build_query($params) : '')); $response->setPrivate(); $response->setMaxAge(0); $response->setSharedMaxAge(0); $response->headers->addCacheControlDirective('must-revalidate', true); $response->headers->addCacheControlDirective('no-store', true); $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $request = $event->getRequest(); if (!in_array($request->getRequestFormat(), array('soap', 'xml'))) { return; } elseif ('xml' === $request->getRequestFormat() && '_webservice_call' !== $request->attributes->get('_route')) { return; } $attributes = $request->attributes; if (!($webservice = $attributes->get('webservice'))) { return; } if (!$this->container->has(sprintf('besimple.soap.context.%s', $webservice))) { return; } // hack to retrieve the current WebService name in the controller $request->query->set('_besimple_soap_webservice', $webservice); $exception = $event->getException(); if ($exception instanceof \SoapFault) { $request->query->set('_besimple_soap_fault', $exception); } parent::onKernelException($event); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if ($exception instanceof AccessDeniedException && $this->admin->inAdmin() && $event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { $response = new RedirectResponse($this->router->generate('glory_admin_login')); $event->setResponse($response); } }
protected function ensureResponse($response, GetResponseForExceptionEvent $event) { if ($response instanceof Response) { $event->setResponse($response); } else { $viewEvent = new GetResponseForControllerResultEvent($this->app['kernel'], $event->getRequest(), $event->getRequestType(), $response); $this->app['dispatcher']->dispatch(KernelEvents::VIEW, $viewEvent); if ($viewEvent->hasResponse()) { $event->setResponse($viewEvent->getResponse()); } } }
/** * @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); }
public function onKernelException(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $request = $event->getRequest(); if ('' !== rtrim($request->getPathInfo(), '/')) { return; } $ex = $event->getException(); if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) { return; } $event->setResponse(new RedirectResponse($request->getBaseUrl() . '/' . $request->getPreferredLanguage(array_merge(array($this->defaultLocale), $this->locales)) . $request->getPathInfo())); }
public function handle(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } if (!$this->_handlingExceptions) { return; } $exception = $event->getException(); if (!$this->_handlingHttp404s) { if ($exception instanceof HttpException && $exception->getStatusCode() === 404) { return; } } $this->mail($exception, $event->getRequest(), array(), false); }
/** * Handle the event * * @param GetResponseForExceptionEvent $event event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $exception = $event->getException(); if ($exception instanceof HttpException) { if (500 === $exception->getStatusCode() || 404 === $exception->getStatusCode() && true === $this->handle404 || in_array($exception->getStatusCode(), $this->handleHTTPcodes)) { $this->createMailAndSend($exception, $event->getRequest()); } } else { $sendMail = !in_array(get_class($exception), $this->ignoredClasses); if ($sendMail === true) { $this->createMailAndSend($exception, $event->getRequest()); } } }
public function onKernelException(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $request = $event->getRequest(); if ('' !== rtrim($request->getPathInfo(), '/')) { return; } $ex = $event->getException(); if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) { return; } $locale = false; // if a locale has been specifically set as a query parameter, use it if ($request->query->has('hl')) { $hostLanguage = $request->query->get('hl'); if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) { $locale = $hostLanguage; } } // check if a session exists, and if it contains a locale if ($locale === false && $request->hasPreviousSession()) { $session = $request->getSession(); if ($session->has('_locale')) { $locale = $session->get('_locale'); } } // use accept header for locale matching if sent if ($locale === false && ($languages = $request->getLanguages())) { foreach ($languages as $lang) { if (in_array($lang, $this->availableLocales, true)) { $locale = $lang; break; } } } if ($locale === false) { $locale = $this->defaultLocale; } $request->setLocale($locale); $params = $request->query->all(); unset($params['hl']); $event->setResponse(new RedirectResponse($request->getBaseUrl() . '/' . $locale . '/' . ($params ? '?' . http_build_query($params) : ''))); }
/** * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $request = $event->getRequest(); if ('' !== rtrim($request->getPathInfo(), '/')) { return; } $ex = $event->getException(); if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) { return; } $locale = $this->localeResolver->resolveLocale($request, $this->locales) ?: $this->defaultLocale; $request->setLocale($locale); $params = $request->query->all(); unset($params['hl']); $event->setResponse(new RedirectResponse($request->getBaseUrl() . '/' . $locale . '/' . ($params ? '?' . http_build_query($params) : ''), 301)); }
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); }
/** * Respond with a challenge on access denied exceptions if appropriate. * * On a 403 (access denied), if there are no credentials on the request, some * authentication methods (e.g. basic auth) require that a challenge is sent * to the client. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * The exception event. */ public function onExceptionSendChallenge(GetResponseForExceptionEvent $event) { if (isset($this->challengeProvider) && $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); $exception = $event->getException(); if ($exception instanceof AccessDeniedHttpException && !$this->authenticationProvider->applies($request) && (!isset($this->filter) || $this->filter->appliesToRoutedRequest($request, FALSE))) { $challenge_exception = $this->challengeProvider->challengeException($request, $exception); if ($challenge_exception) { $event->setException($challenge_exception); } } } }
/** * Pass exception handling to authentication manager. * * @param GetResponseForExceptionEvent $event */ public function onException(GetResponseForExceptionEvent $event) { if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { $this->authenticationProvider->handleException($event); } }
/** * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * * @throws \Exception * * @return void */ public function onKernelExceptionView(GetResponseForExceptionEvent $event) { if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { return; } if (!$this->isRestRequest($event->getRequest())) { return; } $result = $event->getException(); $event->setResponse($this->visitResult($result)); $event->stopPropagation(); }
/** * Executes registered error handlers until a response is returned, * in which case it returns it to the client. * * Handler for onKernelException. * * @see error() */ public function onKernelException(GetResponseForExceptionEvent $event) { $errorEvent = new GetResponseForErrorEvent($this, $event->getRequest(), $event->getRequestType(), $event->getException()); $this['dispatcher']->dispatch(SilexEvents::ERROR, $errorEvent); if ($errorEvent->hasResponse()) { $event->setResponse($errorEvent->getResponse()); } }