php Symfony-Component-HttpKernel-Event-GetResponseForExceptionEvent类(方法)实例源码

下面列出了php Symfony-Component-HttpKernel-Event-GetResponseForExceptionEvent 类(方法)源码代码实例,从而了解它的用法。

作者:juuuuu    项目:budge   
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);
 }

作者:Ener-Getic    项目:symfon   
/**
  * 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();
 }

作者:aWEBoLab    项目:tax   
/**
  * 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);
         }
     }
 }

作者:glynnforres    项目:blockad   
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);
     }
 }

作者:ddrozdi    项目:dmap   
/**
  * Replaces the response in case an EnforcedResponseException was thrown.
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ($response = EnforcedResponse::createFromException($event->getException())) {
         // Setting the response stops the event propagation.
         $event->setResponse($response);
     }
 }

作者:bitecode    项目:rest-api-generator-bundl   
public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $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());
         }
     }
     $data = $apiProblem->toArray();
     $response = new JsonResponse($data, $apiProblem->getStatusCode());
     $response->headers->set('Content-Type', 'application/json');
     $event->setResponse($response);
 }

作者:voxsi    项目:minim   
public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     $flattenException = FlattenException::create($exception);
     $msg = 'Something went wrong! (' . $flattenException->getMessage() . ')';
     $event->setResponse(new Response($msg, $flattenException->getStatusCode()));
 }

作者:slayfu    项目:WhoopsBundl   
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));
 }

作者:jarve    项目:jarve   
public function exceptionHandler(GetResponseForExceptionEvent $event)
 {
     if ($event->getException() instanceof PluginException) {
         return;
     }
     $event->setException(new PluginException(sprintf('The plugin `%s` from bundle `%s` [%s] errored.', $this->plugin['plugin'], $this->bundleName, $this->pluginDef->getController()), null, $event->getException()));
 }

作者:Toleranc    项目:Toleranc   
/**
  * @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()));
 }

作者:kgilde    项目:page   
/**
  * @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));
 }

作者:llinde    项目:FrameworkBenchmark   
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;
 }

作者:mglde    项目:coffeetrac   
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;
 }

作者:potoga    项目:mobile-win   
public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ($event->getException() instanceof NotFoundHttpException) {
         $response = new RedirectResponse('karkeu');
         $event->setResponse($response);
     }
 }

作者:pmd    项目:UnifikSystemBundl   
/**
  * Event handler that renders custom pages in case of a NotFoundHttpException (404)
  * or a AccessDeniedHttpException (403).
  *
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ('dev' == $this->kernel->getEnvironment()) {
         return;
     }
     $exception = $event->getException();
     $this->request->setLocale($this->defaultLocale);
     $this->request->setDefaultLocale($this->defaultLocale);
     if ($exception instanceof NotFoundHttpException) {
         $section = $this->getExceptionSection(404, '404 Error');
         $this->core->addNavigationElement($section);
         $unifikRequest = $this->generateUnifikRequest($section);
         $this->setUnifikRequestAttributes($unifikRequest);
         $this->request->setLocale($this->request->attributes->get('_locale', $this->defaultLocale));
         $this->request->setDefaultLocale($this->request->attributes->get('_locale', $this->defaultLocale));
         $this->entityManager->getRepository('UnifikSystemBundle:Section')->setLocale($this->request->attributes->get('_locale'));
         $response = $this->templating->renderResponse('UnifikSystemBundle:Frontend/Exception:404.html.twig', array('section' => $section));
         $response->setStatusCode(404);
         $event->setResponse($response);
     } elseif ($exception instanceof AccessDeniedHttpException) {
         $section = $this->getExceptionSection(403, '403 Error');
         $this->core->addNavigationElement($section);
         $unifikRequest = $this->generateUnifikRequest($section);
         $this->setUnifikRequestAttributes($unifikRequest);
         $response = $this->templating->renderResponse('UnifikSystemBundle:Frontend/Exception:403.html.twig', array('section' => $section));
         $response->setStatusCode(403);
         $event->setResponse($response);
     }
 }

作者:ZerGabrie    项目:phpb   
/**
  * 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);
 }

作者:boske    项目:BeSimpleSoapBundl   
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);
 }

作者:Garda    项目:cookWithMeAP   
public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     $response = new JsonResponse(['errorMessage' => $event->getException()->getMessage(), 'stackTrace' => $event->getException()->getTraceAsString()]);
     if ($exception instanceof HttpExceptionInterface) {
         $response->setStatusCode($exception->getStatusCode());
     } else {
         if ($exception instanceof AuthenticationException) {
             $response->setData(['errorMessage' => 'You don\'t have permissions to do this.']);
             $response->setStatusCode(Response::HTTP_FORBIDDEN);
         } else {
             if ($exception instanceof InvalidFormException) {
                 $errors = [];
                 foreach ($exception->getForm()->getErrors(true) as $error) {
                     if ($error->getOrigin()) {
                         $errors[$error->getOrigin()->getName()][] = $error->getMessage();
                     }
                 }
                 $data = ['errors' => $errors, 'errorMessage' => ''];
                 $response->setData($data);
                 $response->setStatusCode(Response::HTTP_BAD_REQUEST);
             } else {
                 $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
             }
         }
     }
     $event->setResponse($response);
 }

作者:ddrozdi    项目:dmap   
/**
  * Catches failed parameter conversions and throw a 404 instead.
  *
  * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  */
 public function onException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     if ($exception instanceof ParamNotConvertedException) {
         $event->setException(new NotFoundHttpException($exception->getMessage(), $exception));
     }
 }

作者:cowten    项目:cowtent-applicatio   
/**
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     $code = $exception->getCode();
     $debug = array('class' => get_class($exception), 'code' => $code, 'message' => $exception->getMessage());
     $this->logger->error(print_r($debug, true));
     // HttpExceptionInterface est un type d'exception spécial qui
     // contient le code statut et les détails de l'entête
     if ($exception instanceof NotFoundHttpException) {
         $data = array('error' => array('code' => $code ? $code : -3, 'message' => $exception->getMessage()));
         $response = new JsonResponse($data);
         $response->setStatusCode($exception->getStatusCode());
         $response->headers->replace($exception->getHeaders());
         $response->headers->set('Content-Type', 'application/json');
     } elseif ($exception instanceof HttpExceptionInterface) {
         $data = array('error' => array('code' => $code ? $code : -2, 'message' => $exception->getMessage()));
         $response = new JsonResponse($data);
         $response->setStatusCode($exception->getStatusCode());
         $response->headers->replace($exception->getHeaders());
         $response->headers->set('Content-Type', 'application/json');
     } else {
         $data = array('error' => array('code' => $code ? $code : -1, 'message' => 'Internal Server Error / ' . $exception->getMessage()));
         $response = new JsonResponse($data);
         $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
     }
     // envoie notre objet réponse modifié à l'évènement
     $event->setResponse($response);
 }


问题


面经


文章

微信
公众号

扫码关注公众号