Search code examples
symfonyapache2fosrestbundle

Return abitrary http code in symfony


Using symfony standard edition 2.6 and the FOSRestBundle, I'm throwing HttpException in my controller:

throw new HttpException(525, "An error occured", $e);

In the browser, it shows as an error 500.

When using 509 for instance, it's showing as 509.

What's wrong ?


Solution

  • It seems that it's a bug from symfony.

    In the HttpKernel->handleException() method, only the status code is set:

    if ($e instanceof HttpExceptionInterface) {
      // keep the HTTP status code and headers
      $response->setStatusCode($e->getStatusCode());
    

    Then it tries to detect the statusText from the static array Response::$statusText but your code (530) is not defined, so it sets an empty text:

      if (null === $text) {
            $this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : '';
    
            return $this;
        }
    
        if (false === $text) {
            $this->statusText = '';
    
            return $this;
        }
    

    I'm not sure that according to the from the RFC the statusText (== reapon-phrase) is mandatory: it seems that is can be a space, so HTTP/1.1 525 (with two space) should be valid.

    I think that apache (or the apache php binding) is modifying the status code when no status text is provided.