public function onKernelException(GetResponseForExceptionEvent $event) { if ($event->getException() instanceof AccessDeniedException) { $event->setResponse(new JsonResponse(['error' => 'access_denied'])); $event->stopPropagation(); } }
/** * 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(); } }
public function onKernelException(GetResponseForExceptionEvent $event) { if ($event->getException() instanceof EntityNotFoundException) { $response = new Response('Entity not found', Response::HTTP_NOT_FOUND); $event->setResponse($response); $event->stopPropagation(); } }
/** * @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 $responseForExceptionEvent) { $request = $responseForExceptionEvent->getRequest(); if ($responseForExceptionEvent->getException() instanceof MethodNotAllowedHttpException && 'OPTIONS' === $request->getMethod()) { $response = new JsonResponse(null, Response::HTTP_OK); $responseForExceptionEvent->setResponse($response); $responseForExceptionEvent->stopPropagation(); } }
/** * Handle boot initialisation exceptions. * * @param GetResponseForExceptionEvent $event */ public function onBootException(GetResponseForExceptionEvent $event) { if ($this->isProfilerRequest($event->getRequest())) { return; } $exception = $event->getException(); if ($exception instanceof BootException) { $event->setResponse($exception->getResponse()); $event->stopPropagation(); } }
/** * @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(); }
/** * 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(); }
public function onKernelException(GetResponseForExceptionEvent $event) { /** @var OrchestratorException $exception */ $exception = $event->getException(); $exceptionId = KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME . '-' . md5(microtime()); if (!$exception instanceof OrchestratorException) { return; } $event->stopPropagation(); $data = array(); $data["status"] = $exception->getType(); $data["message"] = $exception->getMessage(); // getMessage $data["code"] = $exception->getExceptionCode(); // getExceptionCode $response = new JsonResponse($data); $response->headers->set('Access-Control-Allow-Origin', '*'); $response->headers->set('Content-Type', 'application/json'); $response->setStatusCode($exception->getStatusCode() ? $exception->getStatusCode() : 400); $this->logger->debug($exception->getMessage(), array('exception' => $exception, 'exceptionId' => $exceptionId)); $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); $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(); }
/** * @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(); }
/** * Handles a kernel exception. * * @param GetResponseForExceptionEvent $event * * @throws \Exception */ public function onKernelException(GetResponseForExceptionEvent $event) { if ($event->getException() instanceof NotFoundHttpException && $this->cmsManagerSelector->isEditor()) { $pathInfo = $event->getRequest()->getPathInfo(); // can only create a CMS page, so the '_route' must be null $creatable = !$event->getRequest()->get('_route') && $this->decoratorStrategy->isRouteUriDecorable($pathInfo); if ($creatable) { $response = new Response($this->templating->render('SonataPageBundle:Page:create.html.twig', array('pathInfo' => $pathInfo, 'site' => $this->siteSelector->retrieve(), 'creatable' => $creatable)), 404); $event->setResponse($response); $event->stopPropagation(); return; } } if ($event->getException() instanceof InternalErrorException) { $this->handleInternalError($event); } else { $this->handleNativeError($event); } }
/** * Dispatch and handle the legacy event `frontcontroller.exception` * * @deprecated removal scheduled for 2.0.0 * * @param GetResponseForExceptionEvent $event */ private function handleLegacyExceptionEvent(GetResponseForExceptionEvent $event) { $modinfo = \ModUtil::getInfoFromName($event->getRequest()->attributes->get('_zkModule')); $legacyEvent = new \Zikula\Core\Event\GenericEvent($event->getException(), array('modinfo' => $modinfo, 'type' => $event->getRequest()->attributes->get('_zkType'), 'func' => $event->getRequest()->attributes->get('_zkFunc'), 'arguments' => $event->getRequest()->attributes->all())); $this->dispatcher->dispatch('frontcontroller.exception', $legacyEvent); if ($legacyEvent->isPropagationStopped()) { $event->getRequest()->getSession()->getFlashBag()->add('error', __f('The \'%1$s\' module returned an error in \'%2$s\'. (%3$s)', array($event->getRequest()->attributes->get('_zkModule'), $event->getRequest()->attributes->get('_zkFunc'), $legacyEvent->getArgument('message'))), $legacyEvent->getArgument('httpcode')); $route = $event->getRequest()->server->get('referrer'); $response = new RedirectResponse($route); $event->setResponse($response); $event->stopPropagation(); } }
/** * * @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; } }