NotAcceptableHttpException example



namespace Symfony\Component\HttpKernel\Tests\Exception;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

class NotAcceptableHttpExceptionTest extends HttpExceptionTest
{
    protected function createException(string $message = '', \Throwable $previous = null, int $code = 0, array $headers = []): HttpException
    {
        return new NotAcceptableHttpException($message$previous$code$headers);
    }
}
$result_collection->add($name$route);
    }

    if (count($result_collection)) {
      return $result_collection;
    }

    // We do not throw a     // \Symfony\Component\Routing\Exception\ResourceNotFoundException here     // because we don't want to return a 404 status code, but rather a 406.     $available_formats = static::getAvailableFormats($collection);
    $not_acceptable = new NotAcceptableHttpException("No route found for the specified format $format. Supported formats: " . implode(', ', $available_formats) . '.');
    if ($available_formats) {
      $links = [];
      foreach ($available_formats as $available_format) {
        $url = Url::fromUri($request->getUri()['query' => ['_format' => $available_format]])->toString(TRUE)->getGeneratedUrl();
        $content_type = $request->getMimeType($available_format);
        $links[] = "<$url>; rel=\"alternate\"; type=\"$content_type\"";
      }
      $not_acceptable->setHeaders(['Link' => implode(', ', $links)]);
    }
    throw $not_acceptable;
  }

  

  public function onRespond(ViewEvent $event) {
    $request = $event->getRequest();
    $result = $event->getControllerResult();

    // If this is a render array then we assume that the router went with the     // generic controller and not one with a format. If the format requested is     // not HTML though, we can also assume that the requested format is invalid     // so we provide a 406 response.     if (is_array($result) && $request->getRequestFormat() !== 'html') {
      throw new NotAcceptableHttpException('Not acceptable format: ' . $request->getRequestFormat());
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    $events[KernelEvents::VIEW][] = ['onRespond', -10];
    return $events;
  }

}
$this->assertEquals('500', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new NotFoundHttpException());
        $this->assertEquals('404', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new UnauthorizedHttpException('Basic realm="My Realm"'));
        $this->assertEquals('401', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new BadRequestHttpException());
        $this->assertEquals('400', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new NotAcceptableHttpException());
        $this->assertEquals('406', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new ConflictHttpException());
        $this->assertEquals('409', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new MethodNotAllowedHttpException(['POST']));
        $this->assertEquals('405', $flattened->getStatusCode());

        $flattened = FlattenException::createFromThrowable(new AccessDeniedHttpException());
        $this->assertEquals('403', $flattened->getStatusCode());

        
        $collection->remove($name);
      }
    }

    if (count($collection)) {
      return $collection;
    }

    // We do not throw a     // \Symfony\Component\Routing\Exception\ResourceNotFoundException here     // because we don't want to return a 404 status code, but rather a 406.     throw new NotAcceptableHttpException('No route found for the specified formats ' . implode(' ', $acceptable_mime_types));
  }

}
Home | Imprint | This part of the site doesn't use cookies.