thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

Root exception class (and possibly, its children) not compatible with PHP7+ #325

Open uuf6429 opened 2 years ago

uuf6429 commented 2 years ago

https://github.com/thephpleague/route/blob/b2d82a8b212787f10244e6fa8f94c939560d23bd/src/Http/Exception.php#L30

This breaks on PHP 7+ when a non-\Exception throwable is thrown (e.g. TypeError; see also the type hierarchy).

For best compatibility, the type declaration should be removed. If needs be, ?\Exception / ?\Throwable could be defined via PHPDoc.

Since it's removing a restriction, I don't think this should be considered as a BC break.


On a side note, not sure why it needs to keep a copy of the message: https://github.com/thephpleague/route/blob/b2d82a8b212787f10244e6fa8f94c939560d23bd/src/Http/Exception.php#L20 and https://github.com/thephpleague/route/blob/b2d82a8b212787f10244e6fa8f94c939560d23bd/src/Http/Exception.php#L35


An effective stopgap solution for my use-case:

class InternalServerErrorException extends \League\Route\Http\Exception
{
    protected $status = 500;
    protected $message = 'Internal Server Error';

    /**
     * @throws \ReflectionException
     * @noinspection MagicMethodsValidityInspection
     * @noinspection PhpMissingParentConstructorInspection
     */
    public function __construct(Throwable $previous = null)
    {
        $reflector = new \ReflectionMethod(\Exception::class, '__construct');
        $reflector->invoke($this, $this->message, 0, $previous);
    }
}