本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent::setException方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForExceptionEvent::setException方法的具体用法?PHP GetResponseForExceptionEvent::setException怎么用?PHP GetResponseForExceptionEvent::setException使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
的用法示例。
在下文中一共展示了GetResponseForExceptionEvent::setException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof NotFoundException) {
$event->setException(new NotFoundHttpException($this->getTranslatedMessage($exception), $exception));
} elseif ($exception instanceof UnauthorizedException) {
$event->setException(new AccessDeniedException($this->getTranslatedMessage($exception), $exception));
} elseif ($exception instanceof BadStateException || $exception instanceof InvalidArgumentException) {
$event->setException(new BadRequestHttpException($this->getTranslatedMessage($exception), $exception));
} elseif ($exception instanceof Translatable) {
$event->setException(new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, get_class($exception) . ': ' . $this->getTranslatedMessage($exception), $exception));
}
}
示例3: onKernelException
/**
* @param GetResponseForExceptionEvent $event
* @return bool|void
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
static $handling;
if (true === $handling) {
return false;
}
$handling = true;
$exception = $event->getException();
if ($exception instanceof AccessDeniedHttpException) {
if (StringUtils::startsWith($exception->getMessage(), 'Expression "has_role(\'ROLE_')) {
$message = 'Bu alana erişebilmek için kullanıcı girişi yapılmalıdır';
$statusCode = 401;
} else {
$message = $exception->getMessage();
$statusCode = $exception->getStatusCode();
}
$exception = new HttpException($statusCode, $message, $exception);
$event->setException($exception);
parent::onKernelException($event);
} elseif ($exception instanceof InsufficientAuthenticationException) {
$exception = new AccessDeniedHttpException('Bu alana erişebilmek için geçerli bir kullanıcı kimliği belirtilmelidir', $exception);
$event->setException($exception);
parent::onKernelException($event);
}
$handling = false;
return false;
}
示例4: exceptionHandler
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()));
}
示例5: onJsonRpcException
public function onJsonRpcException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof InvalidRequestException) {
$event->setException(new BadRequestHttpException($exception->getMessage(), $exception));
}
}
示例6: onException
/**
* 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));
}
}
示例7: onKernelException
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);
}
}
示例8: onKernelException
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;
}
示例9: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if (!$exception instanceof ParamValidationFailedException) {
return;
}
$event->setException($this->convertException($exception));
}
示例10: onCoreException
/**
* Handles security related exceptions.
*
* @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance
*/
public function onCoreException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$request = $event->getRequest();
if ($exception instanceof AuthenticationException) {
if (null !== $this->logger) {
$this->logger->info(sprintf('Authentication exception occurred; redirecting to authentication entry point (%s)', $exception->getMessage()));
}
try {
$response = $this->startAuthentication($request, $exception);
} catch (\Exception $e) {
$event->setException($e);
return;
}
} elseif ($exception instanceof AccessDeniedException) {
$token = $this->context->getToken();
if (!$this->authenticationTrustResolver->isFullFledged($token)) {
if (null !== $this->logger) {
$this->logger->info('Access denied (user is not fully authenticated); redirecting to authentication entry point');
}
try {
$response = $this->startAuthentication($request, new InsufficientAuthenticationException('Full authentication is required to access this resource.', $token, 0, $exception));
} catch (\Exception $e) {
$event->setException($e);
return;
}
} else {
if (null !== $this->logger) {
$this->logger->info('Access is denied (and user is neither anonymous, nor remember-me)');
}
try {
if (null !== $this->accessDeniedHandler) {
$response = $this->accessDeniedHandler->handle($request, $exception);
if (!$response instanceof Response) {
return;
}
} else {
if (null === $this->errorPage) {
return;
}
$subRequest = Request::create($this->errorPage, 'get', array(), $request->cookies->all(), array(), $request->server->all());
$subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception);
$response = $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
$response->setStatusCode(403);
}
} catch (\Exception $e) {
if (null !== $this->logger) {
$this->logger->err(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()));
}
$event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
return;
}
}
} else {
return;
}
$event->setResponse($response);
}
示例11: onKernelException
/**
* Maps known exceptions to HTTP exceptions.
*
* @param GetResponseForExceptionEvent $event The event object
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$class = $this->getTargetClass($exception);
if (null === $class) {
return;
}
if (null !== ($httpException = $this->convertToHttpException($exception, $class))) {
$event->setException($httpException);
}
}
示例12: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof HttpException) {
return;
}
$exception = $this->decorateException($exception);
if ($exception) {
$event->setException($exception);
}
}
示例13: onKernelException
/**
* Maps known exceptions to HTTP exceptions.
*
* @param GetResponseForExceptionEvent $event The event object
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$class = get_class($exception);
if (!isset($this->mapper[$class])) {
return;
}
if (null !== ($httpException = $this->convertToHttpException($exception, $this->mapper[$class]))) {
$event->setException($httpException);
}
}
示例14: onKernelException
/**
* 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));
}
}
}
示例15: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof HttpExceptionInterface) {
return;
}
$convertedExceptionClass = $this->findConvertToExceptionClass($exception);
if (!$convertedExceptionClass) {
return;
}
$this->logException($exception);
$convertedException = $this->convertException($exception, $convertedExceptionClass);
$event->setException($convertedException);
}