isServerError 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 {
            
        $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());

        
use Symfony\Component\HttpFoundation\Response;

/** * A policy denying caching of a server error (HTTP 5xx) responses. */
class NoServerError implements ResponsePolicyInterface {

  /** * {@inheritdoc} */
  public function check(Response $response, Request $request) {
    if ($response->isServerError()) {
      return static::DENY;
    }
  }

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