本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent::setResponse方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForExceptionEvent::setResponse方法的具体用法?PHP GetResponseForExceptionEvent::setResponse怎么用?PHP GetResponseForExceptionEvent::setResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
的用法示例。
在下文中一共展示了GetResponseForExceptionEvent::setResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$logRef = uniqid();
$exception = $event->getException();
if ($exception instanceof NotFoundHttpException) {
$event->setResponse(new VndErrorResponse("Not found", Response::HTTP_NOT_FOUND));
return;
}
if ($exception instanceof AuthenticationException) {
$event->setResponse(new VndErrorResponse("Unauthorized", Response::HTTP_UNAUTHORIZED));
return;
}
new VndErrorResponse("Authentication Failure", Response::HTTP_UNAUTHORIZED);
$code = $exception->getCode();
if (strlen($code) !== 3) {
$this->fallback($message, $code, $logRef, $exception);
} else {
switch (substr($code, 0, 1)) {
case '4':
$message = 'Input Error';
$this->logger->notice("Input error [logref {$logRef}]: " . $exception->__toString());
break;
case '5':
$message = 'Server Error';
$this->logger->error("Runtime error [logref {$logRef}]: " . $exception->__toString());
break;
default:
$this->fallback($message, $code, $logRef, $exception);
}
}
$event->setResponse(new VndErrorResponse($message, $code, $logRef));
}
示例2: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = new JsonResponse();
$detail = sprintf("Message: %s\nFile: %s:%s", $exception->getMessage(), $exception->getFile(), $exception->getLine());
$data = ['type' => '#0', 'code' => 0, 'title' => 'Internal Server Error', 'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR, 'detail' => $detail];
$response->setStatusCode(JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
$response->setData($data);
if ($exception instanceof HttpExceptionInterface) {
$response->headers->replace($exception->getHeaders());
}
if ($exception instanceof HttpException) {
$data = ['type' => '#' . $exception->getCode(), 'code' => $exception->getCode(), 'title' => $exception->getMessage(), 'status' => JsonResponse::HTTP_BAD_REQUEST, 'detail' => $exception->getDetails()];
$response->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
$response->setData($data);
}
if ($exception instanceof AccessDeniedHttpException) {
$event->setResponse(new Response("", 403));
} else {
$event->setResponse($response);
}
if (!in_array($this->kernel->getEnvironment(), array('dev', 'test'))) {
unset($data['detail']);
}
}
示例3: onKernelException
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()));
}
示例4: onKernelException
/**
* 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);
}
}
示例5: ensureResponse
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());
}
}
}
示例6: onSilexError
public function onSilexError(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof BadResponseException && false !== strpos($exception->getMessage(), '[status code] 401')) {
$event->setResponse(new Response('Unauthorized', Response::HTTP_UNAUTHORIZED));
return;
}
if ($exception instanceof BadResponseException && false !== strpos($exception->getMessage(), '[status code] 403')) {
$event->setResponse(new Response('Forbidden', Response::HTTP_FORBIDDEN));
return;
}
}
示例7: checkRedirectException
public function checkRedirectException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof ExceptionRedirectException) {
$response = RedirectResponse::create($exception->getUrl(), $exception->getStatusCode());
$event->setResponse($response);
} elseif ($exception instanceof AuthenticationException) {
// Redirect to the login template
$response = RedirectResponse::create($this->urlManager->viewUrl($exception->getLoginTemplate()));
$event->setResponse($response);
}
}
示例8: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof NotFoundHttpException) {
// we don't need to explicitly set 404 status, HttpKernel will do this for us.
if ($this->app['errorPage.notFound'] && ($page = \PageQuery::create()->findPk($this->app['errorPage.notFound']))) {
$generator = AbstractGenerator::create($this->app, $page->getActivePageRevision());
$event->setResponse($generator->render());
} else {
$event->setResponse(new Response('Page not found'));
}
}
}
示例9: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof ValidationException) {
$event->setResponse($this->templating->renderResponse('ICupPublicSiteBundle:Errors:' . strtolower($exception->getMessage()) . '.html.twig', array('redirect' => $this->router->generate('_icup'))));
$this->logger->addError("ValidationException " . $exception->getMessage() . ": " . $exception->getDebugInfo() . ' - ' . $exception->getFile() . ':' . $exception->getLine());
} elseif ($exception instanceof RedirectException) {
$this->logger->addDebug("Handling RedirectException");
$response = $exception->getResponse();
$response->setStatusCode(200);
$event->setResponse($response);
}
}
示例10: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof PayloadValidationException) {
$response = new JsonResponse($exception->getErrors(), JsonResponse::HTTP_BAD_REQUEST);
$event->setResponse($response);
return;
} else {
if ($exception instanceof HttpException) {
$response = new JsonResponse(['message' => $exception->getMessage()], $exception->getStatusCode());
$event->setResponse($response);
return;
}
}
}
示例11: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->getException() instanceof NotFoundHttpException) {
$response = new RedirectResponse('karkeu');
$event->setResponse($response);
}
}
示例12: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($this->env != "prod") {
$event->setException($exception);
return;
}
try {
$code = 404;
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
$code = 404;
} else {
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException) {
$code = 403;
}
}
$file = $code;
if ($this->env != "prod") {
//$file = $file . '.' . $this->env;
}
$file = $file . '.html.twig';
$template = $this->siteManager->getTemplate("portal");
$template = "SymbbTemplateDefaultBundle:Exception:" . $file;
$response = new Response($this->templating->render($template, array('status_code' => $code, 'status_text' => $exception->getMessage(), 'exception' => $exception)));
// setup the Response object based on the caught exception
$event->setResponse($response);
} catch (\Exception $exc) {
$event->setException($exception);
}
// you can alternatively set a new Exception
// $exception = new \Exception('Some special exception');
// $event->setException($exception);
}
示例13: onKernelException
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);
}
示例14: onKernelException
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);
}
示例15: onKernelException
/**
* @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);
}