Skip to content

[DI] Allow get available services from service locator #23915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2017

Conversation

Koc
Copy link
Contributor

@Koc Koc commented Aug 17, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Sometimes we are using service locators and throw context specific exceptions if service not found inside it. Would be nice inform user about available services in our custom exception, like:

try {
    return $this->transports->get($transport);
} catch (NotFoundExceptionInterface $e) {
    $availableTransports = method_exists($e, 'getAlternatives') ? $e->getAlternatives() : [];

    throw TransportNotFoundException::create($transport, $availableTransports, $e);
}

@Koc Koc force-pushed the allow-get-alternatives branch from c47ffc3 to 4993b1c Compare August 17, 2017 12:23
@linaori
Copy link
Contributor

linaori commented Aug 17, 2017

This is a nice addition to ease debugging I suppose. I just don't think this is how you should handle these errors runtime though, might want to solve it without service locator?

@Koc
Copy link
Contributor Author

Koc commented Aug 17, 2017

Service locator is needed here. TransportNotFoundException should be handled by developer during coding/configuration. The problem is we throwing some specific exception and message of this exception can suggest available services. But it cann't without getting private $alternatives.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Aug 18, 2017
@fabpot
Copy link
Member

fabpot commented Aug 18, 2017

Thank you @Koc.

@fabpot fabpot merged commit 4993b1c into symfony:3.4 Aug 18, 2017
fabpot added a commit that referenced this pull request Aug 18, 2017
… (Koc)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Allow get available services from service locator

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the 3.4,
  legacy code removals go to the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

Sometimes we are using service locators and throw context specific exceptions if service not found inside it. Would be nice inform user about available services in our custom exception, like:

```php
try {
    return $this->transports->get($transport);
} catch (NotFoundExceptionInterface $e) {
    $availableTransports = method_exists($e, 'getAlternatives') ? $e->getAlternatives() : [];

    throw TransportNotFoundException::create($transport, $availableTransports, $e);
}
```

Commits
-------

4993b1c Allow to get alternatives when ServiceNotFoundException occurs.
This was referenced Oct 18, 2017
@Koc Koc deleted the allow-get-alternatives branch May 29, 2019 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants