isClientError example

        $e = $event->getThrowable();

        if (!$event->hasResponse()) {
            $this->finishRequest($request$type);

            throw $e;
        }

        $response = $event->getResponse();

        // the developer asked for a specific status code         if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
            // ensure that we actually have an error response             if ($e instanceof HttpExceptionInterface) {
                // keep the HTTP status code and headers                 $response->setStatusCode($e->getStatusCode());
                $response->headers->add($e->getHeaders());
            } else {
                $response->setStatusCode(500);
            }
        }

        try {
            
else {
      $this->assertLessThanOrEqual(2, count($cache_items));
      $found_cached_200_response = FALSE;
      $other_cached_responses_are_4xx = TRUE;
      foreach ($cache_items as $cache_item) {
        $cached_data = unserialize($cache_item->data);
        if (!isset($cached_data['#cache_redirect'])) {
          $cached_response = $cached_data['#response'];
          if ($cached_response->getStatusCode() === 200) {
            $found_cached_200_response = TRUE;
          }
          elseif (!$cached_response->isClientError()) {
            $other_cached_responses_are_4xx = FALSE;
          }
          $this->assertNotInstanceOf(ResourceResponseInterface::class$cached_response);
          $this->assertInstanceOf(CacheableResponseInterface::class$cached_response);
        }
      }
      $this->assertTrue($found_cached_200_response);
      $this->assertTrue($other_cached_responses_are_4xx);
    }

    // Sort the serialization data first so we can do an identical comparison
$other_cached_responses_are_4xx = TRUE;
    foreach ($cache_items as $cache_item) {
      $cached_data = unserialize($cache_item->data);

      // We might be finding cache redirects when querying like this, so ensure       // we only inspect the actual cached response to see if it got flattened.       if (!isset($cached_data['#cache_redirect'])) {
        $cached_response = $cached_data['#response'];
        if ($cached_response->getStatusCode() === 200) {
          $found_cached_200_response = TRUE;
        }
        elseif (!$cached_response->isClientError()) {
          $other_cached_responses_are_4xx = FALSE;
        }
        $this->assertNotInstanceOf(ResourceResponse::class$cached_response);
        $this->assertInstanceOf(CacheableResponseInterface::class$cached_response);
      }
    }
    $this->assertSame($dynamic_cache !== 'UNCACHEABLE' || isset($dynamic_cache_label_only) && $dynamic_cache_label_only !== 'UNCACHEABLE', $found_cached_200_response);
    $this->assertTrue($other_cached_responses_are_4xx);

    // Not only assert the normalization, also assert deserialization of the     // response results in the expected object.
$request_time = $request->server->get('REQUEST_TIME');
    // The response passes all of the above checks, so cache it. Page cache     // entries default to Cache::PERMANENT since they will be expired via cache     // tags locally. Because of this, page cache ignores max age.     // - Get the tags from CacheableResponseInterface per the earlier comments.     // - Get the time expiration from the Expires header, rather than the     // interface, but see https://www.drupal.org/node/2352009 about possibly     // changing that.     $expire = 0;
    // 403 and 404 responses can fill non-LRU cache backends and generally are     // likely to have a low cache hit rate. So do not cache them permanently.     if ($response->isClientError()) {
      // Cache for an hour by default. If the 'cache_ttl_4xx' setting is       // set to 0 then do not cache the response.       $cache_ttl_4xx = Settings::get('cache_ttl_4xx', 3600);
      if ($cache_ttl_4xx > 0) {
        $expire = $request_time + $cache_ttl_4xx;
      }
    }
    // The getExpires method could return NULL if Expires header is not set, so     // the returned value needs to be checked before calling getTimestamp.     elseif ($expires = $response->getExpires()) {
      $date = $expires->getTimestamp();
      

  public function onRespond(ResponseEvent $event) {
    if (!$event->isMainRequest()) {
      return;
    }

    $response = $event->getResponse();
    if (!$response instanceof CacheableResponseInterface) {
      return;
    }

    if ($response->isClientError()) {
      $http_4xx_response_cacheability = new CacheableMetadata();
      $http_4xx_response_cacheability->setCacheTags(['4xx-response']);
      $response->addCacheableDependency($http_4xx_response_cacheability);
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    // Priority 10, so that it runs before FinishResponseSubscriber, which will
        $e = $event->getThrowable();

        if (!$event->hasResponse()) {
            $this->finishRequest($request$type);

            throw $e;
        }

        $response = $event->getResponse();

        // the developer asked for a specific status code         if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
            // ensure that we actually have an error response             if ($e instanceof HttpExceptionInterface) {
                // keep the HTTP status code and headers                 $response->setStatusCode($e->getStatusCode());
                $response->headers->add($e->getHeaders());
            } else {
                $response->setStatusCode(500);
            }
        }

        try {
            

        $response = new Response('', 200);
        $this->assertTrue($response->isOk());

        $response = new Response('', 404);
        $this->assertFalse($response->isOk());
    }

    public function testIsServerOrClientError()
    {
        $response = new Response('', 404);
        $this->assertTrue($response->isClientError());
        $this->assertFalse($response->isServerError());

        $response = new Response('', 500);
        $this->assertFalse($response->isClientError());
        $this->assertTrue($response->isServerError());
    }

    public function testHasVary()
    {
        $response = new Response();
        $this->assertFalse($response->hasVary());

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