Skip to content

Commit

Permalink
bug #35993 Remove int return type from FlattenException::getCode (wuc…
Browse files Browse the repository at this point in the history
…dbm)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

Remove int return type from FlattenException::getCode

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | - <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | - <!-- required for new features -->

Heya,

So my project died with `Return value of Symfony\Component\ErrorHandler\Exception\FlattenException::getCode() must be of the type int, string returned` symfony error page. Quite unexpected.

Turns out it's a `Doctrine\DBAL\Driver\PDOException` with code `42S02` and message `SQLSTATE[42S02]: Base table or view not found: 1146 Table 'some_db.some_table' doesn't exist`.

This is because I use the `FlattenException` class to serialize errors and store elsewhere, just more convenient to quickly check for any errors. I guess noone has stumbled upon a PDOException /  `FlattenException::getCode` before.

https://www.php.net/manual/en/throwable.getcode.php

`Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException).`

Commits
-------

0f22e07 Remove int return type from FlattenException::getCode
  • Loading branch information
fabpot committed Mar 9, 2020
2 parents ff30b43 + 0f22e07 commit d8d7357
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Expand Up @@ -206,7 +206,7 @@ public function setMessage($message): self
return $this;
}

public function getCode(): int
public function getCode()
{
return $this->code;
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\ErrorHandler\Tests\Fixtures\StringErrorCodeException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Expand Down Expand Up @@ -197,6 +198,15 @@ public function testFile(\Throwable $exception)
$this->assertSame($exception->getFile(), $flattened->getFile());
}

/**
* @dataProvider stringAndIntDataProvider
*/
public function testCode(\Throwable $exception)
{
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame($exception->getCode(), $flattened->getCode());
}

/**
* @dataProvider flattenDataProvider
*/
Expand Down Expand Up @@ -238,6 +248,14 @@ public function flattenDataProvider(): array
];
}

public function stringAndIntDataProvider(): array
{
return [
[new \Exception('test1', 123)],
[new StringErrorCodeException('test2', '42S02')],
];
}

public function testArguments()
{
if (\PHP_VERSION_ID >= 70400) {
Expand Down
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\ErrorHandler\Tests\Fixtures;

class StringErrorCodeException extends \Exception
{

public function __construct(string $message, string $code) {
parent::__construct($message);
$this->code = $code;
}

}

0 comments on commit d8d7357

Please sign in to comment.