/**
  * @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()));
 }
 protected function assertResponse(GetResponseForExceptionEvent $event, $content, $code)
 {
     $response = $event->getResponse();
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertSame($content, $response->getContent());
     $this->assertSame($code, $response->getStatusCode());
 }
 /**
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ($event->getResponse() !== null) {
         return;
     }
     $exception = $event->getException();
     if (!$exception instanceof OAuthException) {
         return;
     }
     $response = new JsonResponse(["code" => $exception->getCode(), "error" => $exception->getMessage()]);
     $event->setResponse($response);
 }
Esempio n. 4
0
 /**
  * @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 (false == $event->getException() instanceof InteractiveRequestInterface) {
         return;
     }
     $interactiveRequest = $event->getException();
     if ($interactiveRequest instanceof SymfonyResponseInteractiveRequest) {
         $event->setResponse($interactiveRequest->getResponse());
     } elseif ($interactiveRequest instanceof ResponseInteractiveRequest) {
         $event->setResponse(new Response($interactiveRequest->getContent()));
     } elseif ($interactiveRequest instanceof RedirectUrlInteractiveRequest) {
         $event->setResponse(new RedirectResponse($interactiveRequest->getUrl()));
     }
     if ($event->getResponse()) {
         if (false == $event->getResponse()->headers->has('X-Status-Code')) {
             $event->getResponse()->headers->set('X-Status-Code', $event->getResponse()->getStatusCode());
         }
         return;
     }
     $ro = new \ReflectionObject($interactiveRequest);
     $event->setException(new LogicException(sprintf('Cannot convert interactive request %s to symfony response.', $ro->getShortName()), null, $interactiveRequest));
 }
 /**
  * @param GetResponseForExceptionEvent $event
  **/
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ('html' === $this->responseFormat) {
         return;
     }
     $exception = $event->getException();
     $response = $event->getResponse();
     $response->setContent($this->serializer->serialize($exception->getMessage(), array(), $this->responseFormat));
     if ($exception instanceof HttpExceptionInterface) {
         $response->setStatusCode($exception->getStatusCode());
         $response->headers->replace($exception->getHeaders());
     } else {
         $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
     }
 }
 /**
  * Handles a kernel exception and returns a relevant response.
  * Aims to deliver content to the user that explains the exception, rather than falling
  * back on symfony's exception handler which displays a less verbose error message.
  *
  * @param GetResponseForExceptionEvent $event The exception event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     // do nothing if request is no preview request
     if (!$event->getRequest()->get('preview', false)) {
         return;
     }
     $ex = $event->getException();
     $code = 500;
     $previousContent = $event->getResponse();
     $content = $previousContent !== null ? $previousContent->getContent() : '';
     $statusTexts = Response::$statusTexts;
     $statusText = isset($statusTexts[$code]) ? $statusTexts[$code] : '';
     $exception = FlattenException::create($ex, $code);
     // log exception
     $this->logger->error(sprintf('%s (%s %s)', $ex->getMessage(), $code, $statusText));
     $responseContent = $this->templateEngine->render($this->findTemplate(), ['status_code' => $code, 'status_text' => $statusText, 'exception' => $exception, 'logger' => null, 'currentContent' => $content]);
     $event->setResponse(new Response($responseContent));
 }
 /**
  * @test
  */
 public function logrefInResponseAndLogMatch()
 {
     foreach ([400, 500] as $code) {
         $logref = null;
         $logger = $this->getMockForAbstractClass('Psr\\Log\\LoggerInterface');
         $logger->expects($this->once())->method($this->anything())->with($this->callback(function ($message) use(&$logref) {
             $matches = [];
             if (preg_match('/logref ([a-z0-9]*)/', $message, $matches)) {
                 $logref = $matches[1];
                 return true;
             }
             return false;
         }));
         /** @var LoggerInterface $logger */
         $this->exceptionListener->setLogger($logger);
         $this->codeProperty->setValue($this->exception, $code);
         $this->exceptionListener->onKernelException($this->event);
         $response = $this->event->getResponse();
         $this->assertEquals($logref, json_decode($response->getContent())->logref);
     }
 }
 /**
  * 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();
 }
 /**
  * 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();
 }
Esempio n. 11
0
 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);
 }
Esempio n. 12
0
 /**
  * Sets a new response to $event
  * @param GetResponseForExceptionEvent $event   The event
  * @param int $statusCode                       The status code of the new response
  * @param string $message                       Optional, the message of the new response
  */
 private function setNewResponse(GetResponseForExceptionEvent $event, $statusCode, $message = '')
 {
     $event->setResponse(new Response());
     $event->getResponse()->setStatusCode($statusCode, $message);
 }
 public function onException(GetResponseForExceptionEvent $event)
 {
     $key = $this->getRequestKey($event->getRequest());
     $this->errors++;
     $this->calls[$key] = array_merge($this->calls[$key], array('response' => $event->getResponse(), "error" => $event->getException(), "stop" => microtime(true)));
     $this->finishEvent($key);
 }