Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added statusCode to parent::_contruct #23

Closed
wants to merge 1 commit into from

Conversation

xtrasmal
Copy link

I ran into a case where I got a InvalidArgumentException(code: 0) with the message:
The HTTP status code "0" is not valid while building the response for an exception of the type HttpException.

As you can see, at the moment, the parent is getting the $code variable, which is not set.
The thing that is being used is $statusCode.

This leads to the following:

$exception->getCode results in 0
$exception->getStatusCode results in a http-status code, like 404.

Expected was the following:

$exception->getCode results in 404
$exception->getStatusCode results 404.

This problem only happens when people have a custom exception handler that expects that the inherited getCode method form the extended parent Exception class can be called...which in this case is being set to 0, even though it is a HttpException.

So my proposed solution is to change the constructor
from:

    public function __construct(int $statusCode, string $message = null, \Throwable $previous = null, array $headers = [], ?int $code = 0)
    {
        $this->statusCode = $statusCode;
        $this->headers = $headers;

        parent::__construct($message, $code, $previous);
    }

to:

    public function __construct(int $statusCode, string $message = null, \Throwable $previous = null, array $headers = [], ?int $code = 0)
    {
        $this->statusCode = $statusCode;
        $this->headers = $headers;

        parent::__construct($message, $statusCode, $previous);
    }

I ran into a case where I got a `InvalidArgumentException(code: 0)` with the message:
`The HTTP status code "0" is not valid` while building the response for an exception of the type HttpException. 

As you can see, at the moment, the parent is getting the $code variable, which is not set. 
The thing that is being used is `$statusCode`. 

This leads to the following:

`$exception->getCode` results in 0
`$exception->getStatusCode` results in a http-status code, like 404.

Expected was the following:

`$exception->getCode` results in 404
`$exception->getStatusCode` results 404.

This problem only happens when people have a custom exception handler that expects that the inherited `getCode` method form the extended parent `Exception` class can be called...which in this case is being set to `0`, even though it is a HttpException.

So my proposed solution is to change the constructor 
from:

```
    public function __construct(int $statusCode, string $message = null, \Throwable $previous = null, array $headers = [], ?int $code = 0)
    {
        $this->statusCode = $statusCode;
        $this->headers = $headers;

        parent::__construct($message, $code, $previous);
    }
```

to: 

```
    public function __construct(int $statusCode, string $message = null, \Throwable $previous = null, array $headers = [], ?int $code = 0)
    {
        $this->statusCode = $statusCode;
        $this->headers = $headers;

        parent::__construct($message, $statusCode, $previous);
    }
```
@symfony-bot
Copy link

symfony-bot bot commented Aug 26, 2019

Thanks for your pull request! We love contributions.

However, this repository is what we call a "subtree split": a read-only copy of one directory of the main Symfony repository. It is used by Composer to allow developers to depend on specific Symfony components.

If you want to contribute, you should instead open a pull request on the main repository:

https://github.com/symfony/symfony

Thank you for your contribution!

PS: if you haven't already, please add tests, and beware that bug fixes should be submitted on the lowest maintained branch where they apply; only features should be submitted against the master branch.

@symfony-bot symfony-bot bot closed this Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant