Exceptions directly thrown by the container MUST implement the
Psr\Container\Exception\ContainerExceptionInterface
.
A call to the get
method with a non-existing id SHOULD throw a
Psr\Container\Exception\NotFoundExceptionInterface
.
/** * @Configuration */ class MyConfiguration { /** * @Bean */ public function myDbService() : DbService {
...
...
throw new MissingDbDriverException();
...
...
} }
Regarding the NotFoundExceptionInterface:
The current text says a container SHOULD throw an exception. Searching through the history of container-interop, I cannot find why we put a "should" here.
I'd like to propose replacing it with a MUST. For instance, it is never ok if a container returns null instead of throwing an exception. Therefore, the need for "MUST". Does anyone has any objection regarding this change?
Regarding the ContainerExceptionInterface:
I'd like to make the opposite move and switch from MUST to SHOULD, as in: "Exceptions directly thrown by the container SHOULD implement the ContainerExceptionInterface".
Furthermore, I'd like to emphasize the "directly" part. For instance, I understand perfectly that a cyclic dependency or a config file typo should throw an exception implementing ContainerExceptionInterface. However, if the user is using a container based on factories (like Disco or PimpleInterop), and if the factory written by the user is throwing an exception, I find it useless to wrap that exception in another exception implementing the ContainerExceptionInterface. To me, it adds noise and nothing more.
The current draft for PSR-11 contains this text relative to exceptions:
1.2 Exceptions
Exceptions directly thrown by the container MUST implement the
Psr\Container\Exception\ContainerExceptionInterface
.A call to the
get
method with a non-existing id SHOULD throw aPsr\Container\Exception\NotFoundExceptionInterface
.
We have already had a discussion regarding the usefulness of ContainerExceptionInterface. Even if I'd like to see it gone, the majority seems to be in favor of keeping it. However, there are 2 other things I'd like to discuss:
Regarding the NotFoundExceptionInterface:
The current text says a container SHOULD throw an exception. Searching through the history of container-interop, I cannot find why we put a "should" here.
I'd like to propose replacing it with a MUST. For instance, it is never ok if a container returns null instead of throwing an exception. Therefore, the need for "MUST". Does anyone has any objection regarding this change?
Regarding the ContainerExceptionInterface:
I'd like to make the opposite move and switch from MUST to SHOULD, as in: "Exceptions directly thrown by the container SHOULD implement the ContainerExceptionInterface".
Furthermore, I'd like to emphasize the "directly" part. For instance, I understand perfectly that a cyclic dependency or a config file typo should throw an exception implementing ContainerExceptionInterface. However, if the user is using a container based on factories (like Disco or PimpleInterop), and if the factory written by the user is throwing an exception, I find it useless to wrap that exception in another exception implementing the ContainerExceptionInterface. To me, it adds noise and nothing more.