On 07/27/2016 12:45 PM, Daniel Hunsaker wrote:


On Wednesday, July 27, 2016 at 9:38:48 AM UTC-6, Takashi Matsuo wrote:


    On Tuesday, July 26, 2016 at 4:01:17 PM UTC-7, Larry Garfield wrote:

        From http://www.php-fig.org/psr/psr-6/#error-handling
        <http://www.php-fig.org/psr/psr-6/#error-handling>

        " For that reason Implementing Libraries MUST NOT throw
        exceptions other than those defined by the interface"

        The spec explicitly says not to throw exceptions except where
        explicitly specified.  We can't recommend throwing an extra
        exception then without it being a BC break in the
        specification, which would affect potentially all current
        users of it.  (There are multiple PSR-6 implementations
        already in the wild, and no doubt projects using them.)

        Whatever the arguments for an exception may be, I don't think
        we can consider them without a BC break.


    Re: BC break

    Is this a really big problem? The changes in PSRs doesn't
    immediately change libraries in the wild. The psr-6 libraries can
    still provide another major version, so that the users of those
    libraries can keep the old behavior with the old stables.


According to the PSR Workflow, a major version bump would require a replacement PSR. Unless I'm not understanding the bylaw correctly.

    Actually there are some libraries already throwing
    InivalidArgumentException:
    https://github.com/tedious/Stash/blob/master/src/Stash/Item.php#L365
    <https://github.com/tedious/Stash/blob/master/src/Stash/Item.php#L365>
    https://github.com/symfony/cache/blob/master/CacheItem.php#L75
    <https://github.com/symfony/cache/blob/master/CacheItem.php#L75>

    Of course yours is using assertion:
    
https://github.com/php-fig/cache-util/blob/master/src/BasicCacheItemTrait.php#L69
    
<https://github.com/php-fig/cache-util/blob/master/src/BasicCacheItemTrait.php#L69>

    If we decide on either of assertion or exception, there will be
    certainly BC breaks in either side, and you can not avoid them :(
    If you can not avoid them, let's think this way; it is just a good
    chance to learn how to change PSRs :)

    Another small benefit of throwing exception is that, it will allow
    libraries to notify users when it is invalid (for example past
    date). `CacheItemInterface::expiresAt()` returns the object, so
    there is no reliable way to notify such errors to users (warnings
    can be easily neglected, you know?).

    Someone unintentionally passes a past date and the value will
    never be cached, then they might keep banging their heads for hours.


For my own part, I would argue that InvalidArgumentException is one used internally by PHP itself, so could be considered an exception to the exception, since it could as easily come from PHP itself as from a cache implementation. Yes, it's being thrown in a new context, but aren't all Exceptions that PHP throws itself implicitly on any list of expected Exceptions one's application might receive?

That points to an issue for the Errata to address, of course, in that PHP might throw exceptions of its own accord, outside the control of the PSR-6 lib itself, and to be aware of the built-in ones when using a PSR-6 implementation. It also points to at least two implementations not actually following the spec as currently written, but again, for my part, their choice can be fairly easily justified, despite actually violating the spec.

So, +1 for InvalidArgumentException and an Errata note that built-in descendants of Exception are implicitly approved on any interface, if for no other reason than PHP itself might throw them before the implementation even gets control.

Er. Except that's not true. PHP itself throws exceptions only in two very specific cases:

* PDO, if configured to do so.
* random_int()/random_bytes(), if no entropy source can be found.

As noted before, a failed type check in PHP 5 is equivalent to

trigger_error('error message', E_RECOVERABLE_ERROR).

In PHP 7, it's equivalent to

throw new TypeError(...);

(TypeError implementing Throwable, but not being a descendant of \Exception.)

See:

https://3v4l.org/XL0Of

Neither of those are InvalidArgumentException.

The spec very explicitly says that cache failures must be non-fatal to the application. Throwing uncaught exceptions is fatal to the application. In fact, the spec explicitly says that exceptions from the underlying engine (PDO, file system, Redis, whatever) must be caught and normalized to just a normal cache failure so that it's non-fatal.

Remember, we're talking about a very narrow problem space: Passing a non-DateTimeInterface object to a method that expects one. There are no conditions where that is anything other than the programmer screwing up, probably very very close to the call itself. (eg, if you call expiresAt('2016-07-29'), then according to the spec you're just wrong and should fix your damned code. <g>)

If the spec were still subject to change then I'd be more open to InvalidArgumentException, as the class does throw that explicitly in a few other places (that are also "Developer, you're just wrong" cases). As this is just an errata, however, I don' think throws are on the table.

--Larry Garfield

--
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/5da8b234-fdbe-2485-e45b-1a122a10c804%40garfieldtech.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to