/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $pathInfo = explode('/', $event->getRequest()->getPathInfo()); if (isset($pathInfo[1]) && in_array($pathInfo[1], $this->locales)) { $event->getRequest()->setLocale($pathInfo[1]); } }
/** * @param GetResponseForExceptionEvent $event * * @return RedirectResponse|null * * @throws \LogicException If the current page is inside the page range */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if (!$exception instanceof OutOfBoundsException) { return; } $pageNumber = $exception->getPageNumber(); $pageCount = $exception->getPageCount(); if ($pageCount < 1) { return; // No pages...so let the exception fall through. } $queryBag = clone $event->getRequest()->query; if ($pageNumber > $pageCount) { $queryBag->set($exception->getRedirectKey(), $pageCount); } elseif ($pageNumber < 1) { $queryBag->set($exception->getRedirectKey(), 1); } else { return; // Super weird, because current page is within the bounds, fall through. } if (null !== ($qs = http_build_query($queryBag->all(), '', '&'))) { $qs = '?' . $qs; } // Create identical uri except for the page key in the query string which // was changed by this listener. // // @see Symfony\Component\HttpFoundation\Request::getUri() $request = $event->getRequest(); $uri = $request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo() . $qs; $event->setResponse(new RedirectResponse($uri)); }
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($event->getRequest()->getMethod()); 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; }
/** * Catches a form AJAX exception and build a response from it. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * The event to process. */ public function onException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); // Render a nice error message in case we have a file upload which exceeds // the configured upload limit. if ($exception instanceof BrokenPostRequestException && $request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) { $this->drupalSetMessage($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]), 'error'); $response = new AjaxResponse(); $status_messages = ['#type' => 'status_messages']; $response->addCommand(new ReplaceCommand(NULL, $status_messages)); $response->headers->set('X-Status-Code', 200); $event->setResponse($response); return; } // Extract the form AJAX exception (it may have been passed to another // exception before reaching here). if ($exception = $this->getFormAjaxException($exception)) { $request = $event->getRequest(); $form = $exception->getForm(); $form_state = $exception->getFormState(); // Set the build ID from the request as the old build ID on the form. $form['#build_id_old'] = $request->get('form_build_id'); try { $response = $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, []); // Since this response is being set in place of an exception, explicitly // mark this as a 200 status. $response->headers->set('X-Status-Code', 200); $event->setResponse($response); } catch (\Exception $e) { // Otherwise, replace the existing exception with the new one. $event->setException($e); } } }
/** * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * * @throws \Exception */ public function onKernelExceptionView(GetResponseForExceptionEvent $event) { if (!$event->getRequest()->attributes->get('is_rest_request')) { return; } $event->setResponse($this->viewDispatcher->dispatch($event->getRequest(), $event->getException())); $event->stopPropagation(); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if (!$exception instanceof StepException) { return; } $reflection = new \ReflectionClass(get_class($exception)); $event->getRequest()->getSession()->getBag('flashes')->add('error', $this->translator->trans('error.recipe.' . lcfirst($reflection->getShortName()))); $targetUrl = $event->getRequest()->getSession()->get(AdminRecipeController::EXCEPTION_TARGET_URL_KEY, $event->getRequest()->getUri()); $event->setResponse(new RedirectResponse($targetUrl)); }
public function onKernelException(GetResponseForExceptionEvent $event) { if (strpos($event->getRequest()->attributes->get('_controller'), '::')) { $implementedInterfaces = class_implements(explode('::', $event->getRequest()->attributes->get('_controller'))[0]); if (in_array(self::SYNC_MARKER_INTERFACE, $implementedInterfaces, TRUE)) { $exception = $event->getException(); $message = sprintf('`%s` [uncaught exception]: throws `%s` (code `%s`) at `%s` line `%s`', get_class($exception), $exception->getMessage(), $exception->getCode(), $exception->getFile(), $exception->getLine()); $this->_logger->error($message, ['exception' => $exception]); } } }
public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->getRequest()->attributes->has('_api_result') && !$this->requestMatcher->matches($event->getRequest())) { return; } $errorFactory = $this->errorFactory; /** @var ErrorInterface $error */ $error = $errorFactory($event->getException()); // Tell Symfony to not use 500 as status code. $response = new JsonResponse(array_merge(['ok' => false, 'error' => $error->getName()], $error->getData()), 200, ['x-status-code' => 200]); $event->setResponse($response); }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (strpos($event->getRequest()->attributes->get('_controller'), 'Api\\Resource') !== false) { $exception = $event->getException(); if ($exception instanceof HttpException) { $statusCode = $exception->getStatusCode(); } else { $statusCode = 500; } $event->setResponse($this->formatter->format($event->getRequest(), [], $statusCode)); } }
/** * If exception type is 404, display the Orchestra 404 node instead of Symfony exception * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if ($event->getException() instanceof DisplayBlockException) { $event->getRequest()->setRequestFormat('fragment.' . $event->getRequest()->getRequestFormat()); $event->setException($event->getException()->getPrevious()); } elseif ($event->getException() instanceof HttpExceptionInterface && '404' == $event->getException()->getStatusCode()) { $this->setCurrentSiteInfo(trim($this->request->getHost(), '/'), trim($this->request->getPathInfo(), '/')); if ($html = $this->getCustom404Html()) { $event->setResponse(new Response($html, 404)); } } }
/** * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if ($exception instanceof HttpException) { return; } $culprit = null; if ($event->getRequest()->attributes->has('_controller')) { $culprit = $event->getRequest()->attributes->get('_controller'); } $metadata = new Metadata(); $metadata->addMetadatum('culprit', $culprit); $this->errorHandler->handleException($exception, $metadata); }
/** * 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) { $ex = $event->getException(); if ($ex instanceof BadRequestException) { $event->setResponse($this->renderer->render($event->getRequest(), $ex)); } }
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); }
/** * @param GetResponseForExceptionEvent $event * * @return string */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->getRequest()->isXmlHttpRequest()) { return self::RESULT_NOT_AJAX; } if (substr($event->getRequest()->getPathInfo(), 0, strlen($this->backendRoutesPrefix)) != $this->backendRoutesPrefix) { return self::RESULT_NOT_BACKEND_REQUEST; } $e = $event->getException(); $response = null; if ($e instanceof AccessDeniedException) { $msg = "Your session has expired and you need to re-login or you don't have privileges to perform given action."; $response = new JsonResponse(array('success' => false, 'message' => T::trans($msg)), 403); $event->setResponse($response); } }
/** * 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) { } }
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) { $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())); }
public function onKernelException(GetResponseForExceptionEvent $event) { static $handling; if (true === $handling) { return false; } $request = $event->getRequest(); if (empty($this->formats[$request->getRequestFormat()]) && empty($this->formats[$request->getContentType()])) { return false; } $handling = true; $exception = $event->getException(); if ($exception instanceof AccessDeniedException) { $exception = new AccessDeniedHttpException('You do not have the necessary permissions', $exception); $event->setException($exception); parent::onKernelException($event); } elseif ($exception instanceof AuthenticationException) { if ($this->challenge) { $exception = new UnauthorizedHttpException($this->challenge, 'You are not authenticated', $exception); } else { $exception = new HttpException(401, 'You are not authenticated', $exception); } $event->setException($exception); parent::onKernelException($event); } $handling = false; }
public function onSilexError(GetResponseForExceptionEvent $event) { if (!$event->getException() instanceof \Bridge_Exception) { return; } $e = $event->getException(); $request = $event->getRequest(); $params = ['account' => null, 'elements' => [], 'message' => $e->getMessage(), 'error_message' => null, 'notice_message' => null, 'file' => $e->getFile(), 'line' => $e->getLine(), 'r_method' => $request->getMethod(), 'r_action' => $request->getRequestUri(), 'r_parameters' => $request->getMethod() == 'GET' ? [] : $request->request->all()]; if ($e instanceof \Bridge_Exception_ApiConnectorNotConfigured) { $params = array_replace($params, ['account' => $this->app['bridge.account']]); $response = new Response($this->app['twig']->render('/prod/actions/Bridge/notconfigured.html.twig', $params), 200, ['X-Status-Code' => 200]); } elseif ($e instanceof \Bridge_Exception_ApiConnectorNotConnected) { $params = array_replace($params, ['account' => $this->app['bridge.account']]); $response = new Response($this->app['twig']->render('/prod/actions/Bridge/disconnected.html.twig', $params), 200, ['X-Status-Code' => 200]); } elseif ($e instanceof \Bridge_Exception_ApiConnectorAccessTokenFailed) { $params = array_replace($params, ['account' => $this->app['bridge.account']]); $response = new Response($this->app['twig']->render('/prod/actions/Bridge/disconnected.html.twig', $params), 200, ['X-Status-Code' => 200]); } elseif ($e instanceof \Bridge_Exception_ApiDisabled) { $params = array_replace($params, ['api' => $e->get_api()]); $response = new Response($this->app['twig']->render('/prod/actions/Bridge/deactivated.html.twig', $params), 200, ['X-Status-Code' => 200]); } else { $response = new Response($this->app['twig']->render('/prod/actions/Bridge/error.html.twig', $params), 200, ['X-Status-Code' => 200]); } $response->headers->set('Phrasea-StatusCode', 200); $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if ($exception instanceof AlreadyLinkedAccount) { $this->session->getFlashBag()->add('error', $this->translator->trans($exception->getMessage())); $url = $this->router->generate('fos_user_profile_edit'); $event->setResponse(new RedirectResponse($url)); } elseif ($exception instanceof MissingEmailException) { $url = $this->router->generate('lc_before_register_twitter'); $event->setResponse(new RedirectResponse($url)); } elseif ($exception instanceof LcEmailException) { $this->session->getFlashBag()->add('error', $this->translator->trans($exception->getMessage())); $url = $this->router->generate('lc_home'); $event->setResponse(new RedirectResponse($url)); } elseif ($exception instanceof \FacebookApiException) { $this->session->getFlashBag()->add('error', $this->translator->trans($exception->getMessage())); $url = $this->router->generate('lc_home'); $event->setResponse(new RedirectResponse($url)); } elseif ($exception instanceof LcFcGbException) { $url = $this->router->generate('lc_link_facebook'); $event->setResponse(new RedirectResponse($url)); } elseif ($exception instanceof LcValidationException) { $this->session->getFlashBag()->add('error', $this->translator->trans($exception->getMessage())); $url = $this->router->generate('fos_user_profile_edit'); $event->setResponse(new RedirectResponse($url)); } elseif ($exception instanceof NotFoundHttpException) { $request = $event->getRequest(); $route = $request->get('_route'); if ($route == 'fos_user_registration_confirm') { $this->session->getFlashBag()->add('error', $this->translator->trans('This e-mail is already confirmed.')); $url = $this->router->generate('fos_user_profile_edit'); $event->setResponse(new RedirectResponse($url)); } } }
/** * Intercept Exceptions and send valid ajax errors * The idea is to NOT send HTML response when an issue(i.e.session expired) is encountered * in an ajax request. * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { // Get the exception object from the received event $exception = $event->getException(); $request = $event->getRequest(); $isAjaxRequest = $request->isXmlHttpRequest(); if ($isAjaxRequest) { $headers = []; // handle session expired if ($exception instanceof AuthenticationException) { $loginUrl = $this->router->generate('fos_user_security_login'); // add custom header to redirect to login page on the client $headers['relogin'] = true; $headers['login-url'] = $loginUrl; $response = new Response('Authentication Required', 200, $headers); } elseif ($exception instanceof AccessDeniedException) { $headers['not-authorized'] = true; $response = new Response('Not authorized', 403, $headers); } else { $responseData = ['status' => false, 'msg' => 'Unknown Issue Encountered', 'data' => ['exception' => ['message' => $exception->getMessage(), 'file' => "{$exception->getFile()}:{$exception->getLine()}", 'trace' => $exception->getTraceAsString()]]]; $response = new Response(json_encode($responseData), 500, $headers); } $event->setResponse($response); $event->stopPropagation(); } }
/** * @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())); }
/** * @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) { $exception = $event->getException(); if (!$exception instanceof BlockadeException) { return; } //try to get a response from one of the resolvers. It //could be a redirect, an access denied page, or anything //really try { foreach ($this->resolvers as $resolver) { $driver = $exception->getDriver(); if (!$driver || !$resolver->supportsDriver($driver)) { continue; } if (!$resolver->supportsException($exception)) { continue; } $request = $event->getRequest(); $response = $resolver->onException($exception, $request); if ($response instanceof Response) { $event->setResponse($response); return; } } //no response has been created by now, so let other //exception listeners handle it return; } catch (\Exception $e) { //if anything at all goes wrong in calling the //resolvers, pass the exception on $event->setException($e); } }
/** * This listener is run when the KernelEvents::EXCEPTION event is triggered * * @param GetResponseForExceptionEvent $event * @return null */ public function on_kernel_exception(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $message = $exception->getMessage(); if ($exception instanceof \phpbb\exception\exception_interface) { $message = $this->language->lang_array($message, $exception->get_parameters()); } if (!$event->getRequest()->isXmlHttpRequest()) { page_header($this->language->lang('INFORMATION')); $this->template->assign_vars(array('MESSAGE_TITLE' => $this->language->lang('INFORMATION'), 'MESSAGE_TEXT' => $message)); $this->template->set_filenames(array('body' => 'message_body.html')); page_footer(true, false, false); $response = new Response($this->template->assign_display('body'), 500); } else { $data = array(); if (!empty($message)) { $data['message'] = $message; } if (defined('DEBUG')) { $data['trace'] = $exception->getTrace(); } $response = new JsonResponse($data, 500); } if ($exception instanceof HttpExceptionInterface) { $response->setStatusCode($exception->getStatusCode()); $response->headers->add($exception->getHeaders()); } $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { $request = $event->getRequest(); /** @var Jsend $configuration */ $configuration = $request->attributes->get('_jsend'); if (!$configuration) { return; } $format = $request->getRequestFormat(); if (!$format || $format === "html") { return; } $error = $event->getException(); if (!$error instanceof HttpException) { return; } $result = array('status' => Jsend::STATUS_ERROR, 'message' => $error->getMessage(), 'code' => $error->getStatusCode()); if ($this->container->getParameter('kernel.environment') == 'dev' && $error->getTrace()) { $result['trace'] = $error->getTrace(); } $strategy = new ExceptionExclusionStrategy(); $context = SerializationContext::create(); $context->addExclusionStrategy($strategy); $content = $this->serializer->serialize($result, $format, $context); $response = new Response($content); if ($format == 'json') { $response->headers->set('Content-Type', 'application/json'); } $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { // only reply to /api URLs if (strpos($event->getRequest()->getPathInfo(), '/api') !== 0) { return; } $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()); } } $response = $this->responseFactory->createResponse($apiProblem); $event->setResponse($response); }
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $whoops = $this->handler->handle($event->getRequest(), $exception->getCode()); $whoopsResponse = $whoops->handleException($exception); $event->setResponse(new Response($whoopsResponse)); }
/** * Handles a 404 error for HTML. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * The event to process. */ public function on404(GetResponseForExceptionEvent $event) { $path = $event->getRequest()->getPathInfo(); $response = $this->createResponse($this->t('Page not found'), $this->t('The requested page "@path" could not be found.', ['@path' => $path]), Response::HTTP_NOT_FOUND); $response->headers->set('Content-type', 'text/html'); $event->setResponse($response); }