Hi people!

This issue has been standing for the last 2 weeks with no activity.

Let's revive this and try to close it.

There is currently one PR made by Larry 
(https://github.com/php-fig/fig-standards/pull/844) proposing to add a 
"MisconfiguredServiceExceptionInterface" to PSR-11. There is support for 
this PR from @GeeH and from the people attending PHPWorld (although we 
don't know who was at this meeting). Larry, could you let us know who 
attended the FIG meeting at PHPWorld?

Also, Matthieu, Daniel and myself are reluctant to add this exception 
because we don't see why you would catch this exception? The question has 
been asked by Matthieu and so far, we haven't had any proposition of a use 
case where catching "MisconfiguredServiceExceptionInterface" would make 
sense. If you can highlight such a use case, please let us know.

Meanwhile, I've been thinking a bit more about what would be the impact of 
adding a "MisconfiguredServiceExceptionInterface" to PSR-11.

Impact of a "MisconfiguredServiceExceptionInterface"

I realized a few days ago that adding this exception could actually be 
detrimental to users and hinder adoption.

Let me give you a sample based on Simplex (the Pimple 3 PSR-11 fork):

The PR reads:

> A call to the get method for a a service that is defined but cannot be 
instantiated due to misconfiguration, missing dependency, or other issue 
related to the container MUST throw a 
Psr\Container\Exception\MisconfiguredServiceExceptionInterface.

Now, let's assume we have a Simplex container declared like this:

$container->set('boom', function() {
    // $container variable is not defined, this will fail.
    return $container->get('foo');
});

$container->set('cache.apcu', function() {
    // Let's say the constructor below will throw an exception because APCU 
is not available on my server.
    return new ApcuCache();
});


Here, I have 2 entries that will cause errors / throw exceptions.

When a call:

- $container->get('boom')
- $container->get('cache.apcu')

should I get a 
'Psr\Container\Exception\MisconfiguredServiceExceptionInterface' ?

This question is hard to answer! Do we consider that what happens in a 
factory is part of the container or not?

I would certainly not wrap `$container->get('boom')` in an exception (so it 
means the container should not be catching Throwable). It makes absolutely 
no sense as this is most typically an "unchecked" exception. And yet, if 
you have a look at the wording of the PR, to me, it qualifies as a 
"misconfiguration" of the container.

So what about the exception thrown by `$container->get('cache.apcu')`? If 
the container wraps it into a 'MisconfiguredServiceExceptionInterface' 
exception, this makes catching the initial exception a lot harder.

Have a look at this other service:

// This service returns an APCU cache when available, a file cache service 
otherwise
$container->set('best.possible.cache', function($container) {
    try {
        return $container->get(cache.apcu);
    } catch (NoApcuExtensionException $e) {
        return $container->get(cache.file);
    }
});

The code above will only work if the container does not catch the 
exceptions. Otherwise, we would need to write something like:

$container->set('best.possible.cache', function($container) {
    try {
        return $container->get(cache.apcu);
    } catch (MisconfiguredServiceExceptionInterface $e) {
        if ($e->getPrevious() instanceof NoApcuExtensionException) {
            return $container->get(cache.file);
        } else {
            throw $e;
        }
    }
});

I'm sure we all can agree that this is a code-smell :)

So we have a problem because the wording surrounding 
'MisconfiguredServiceExceptionInterface' will be extremely hard to get 
right. It should catch enough, but not too much while not saying exactly 
what to catch because this is container specific...

Another issue: depending on the container used, the exceptions are not 
thrown at the same moment. Let's consider a "CyclicDependencyException". It 
is fair to think it should implement the '
MisconfiguredServiceExceptionInterface'. But in compiled containers, this 
exception will be thrown at build time (so not when calling `get`). 
Actually, I expect most exceptions to be thrown at build time for 
compiled/cached containers, and I expect runtime containers to throw very 
few exceptions apart the NotFoundException / DependencyMissingException.

So my feeling is that the 'MisconfiguredServiceExceptionInterface' is 
opening a can of worms. It will be very hard to define precisely what goes 
in and what goes out of this exception.

And we haven't even heard of a practical use case yet.

On the other end, there is *one real impact* to adding this exception:

Existing containers implementaing container-interop might need a *major 
version bump* to be compatible with PSR-11 (because some exceptions that 
would previously have bubbled up could be wrapped inside a '
MisconfiguredServiceExceptionInterface')


So there is no real benefit yet demonstrated, it is hard to define and it 
causes challenge regarding adoption by existing containers. You understood 
my position, I'm very much against the idea of a '
MisconfiguredServiceExceptionInterface'.


Another solution: a simple `MissingDependencyExceptionInterface`


Because the scope of the 'MisconfiguredServiceExceptionInterface' is really 
hard to define, we could simply restrict the scope of this exception to 
"missing dependencies" (that was the idea at the beginning of this thread).

This is a better solution in my opinion because:

- This is directly related to PSR-11 scope: "getting things out of the 
container". We are dealing with "dependency injection container", this 
means an entry can have "dependencies" (it is in the name), and if the 
dependency is missing, it is not illogical to have an exception for this.
- It is really easy to define without ambiguity (no "can of worm" opening 
here)
- It is easy to migrate existing container-interop containers. All those 
containers have such an exception. Indeed, when a dependency is missing, 
the container MUST NOT throw a `NotFoundException`. Therefore, all 
containers are catching the `NotFoundException` and wrapping it in another `
MissingDependencyException`. So implementation is as easy as implementing 
the `MissingDependencyExceptionInterface` in the existing `
MissingDependencyException` of each container.

Yet, there is a drawback here (compared to the current proposal):

We cannot do an "instant migration" of container-interop towards PSR-11 
anymore.

So far, when PSR-11 is adopted, we were considering to release of 
container-interop 1.2 version where Interop\Container\ContainerInterface 
simply extends Psr\Container\ContainerInterface. Shazam! Instantly at the 
release of PSR-11, all container-interop containers would have become 
PSR-11 containers (fastest adoption rate for a PSR ever).

If we add a `MissingDependencyExceptionInterface`, this is no longer 
possible and every container must add support manually.

Finally

We have 3 possible solutions here:

- Adding a `MisconfiguredServiceExceptionInterface` (I explained above why 
I find this idea potentially harmful)
- Adding a `MissingDependencyExceptionInterface` (I believe it does not 
hurt but is mostly useless and preventing us to do "instant migration" of 
container-interop)
- Keep things as they are

You probably understood I prefer the last solution. We haven't even heard 
of a practical use case yet for those exception interfaces (in the 3+ years 
of container-interop, no-one ever complained that this exception interface 
was missing, see: 
https://github.com/container-interop/container-interop/issues/3 ). And if 
we don't add this exception interface, we can do an "instant" migration of 
all container-interop containers to PSR-11 (which is insanely cool even if 
this is not an objective of PSR-11 per-se).

But finally, this boils down to one question: can any one provide a good 
real-world use case to catching one of these 2 exception interfaces?

++
David.

Le mercredi 23 novembre 2016 18:05:20 UTC+1, Matthieu Napoli a écrit :
>
> This way I can catch all container exceptions using ContainerException, or 
> the individual exception I am interested in. In your example, it would not 
> be possible to catch only exceptions raised by the container, as other 
> Exceptions could be raised (if for example pulling service config from 
> database or memory, or using an HTTP request, these services may raise 
> uncaught exceptions). 
>
>
> Again, can we get an actual use case where one would need to catch 
> MisconfiguredException (or whatever the name) separately from \Exception? 
> We still have had no answer to that.
>
> Personally, I would handle missing services differently to a general 
> container error and would be keen to see the spec reflect this
>
>
> NotFoundException is already a thing in the spec, do you have something 
> else in mind?
>

-- 
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/e5984da0-a522-4ebf-8d34-88f094a37c77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to