Skip to content
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

PSR-18: Separate ConnectException from RequestException #2541

Merged
merged 6 commits into from
Jan 19, 2020

Conversation

davidgrayston
Copy link
Contributor

Guzzle version affected: 7.0.0-beta.1

Relates to #2507 / php-http/httplug#155

Description

Update exception hierarchy to conform with https://www.php-fig.org/psr/psr-18/meta/

The domain exceptions NetworkExceptionInterface and RequestExceptionInterface define a contract very similar to each other. The chosen approach is to not let them extend each other because inheritance does not make sense in the domain model. A RequestExceptionInterface is simply not a NetworkExceptionInterface.

Changed

  • ConnectException now extends TransferException instead of RequestException

    Note: request, response and handler context methods have been moved into traits for reuse - there might be a neater/preferable way

New hierarchy:

\RuntimeException
-- TransferException
-- -- ConnectException (Psr\Http\Client\NetworkExceptionInterface)
-- -- RequestException (Psr\Http\Client\RequestExceptionInterface)
-- -- -- TooManyRedirectsException
-- -- -- BadResponseException
-- -- -- -- ClientException
-- -- -- -- ServerException

I've tried to do this with as few breaking changes as possible - you could instead introduce a new exception that implements Psr\Http\Client\RequestExceptionInterface which would extend RequestException like this (which would be non-breaking):

\RuntimeException
-- TransferException
-- -- RequestException
-- -- -- ConnectException (Psr\Http\Client\NetworkExceptionInterface)
-- -- -- RequestFailedException (Psr\Http\Client\RequestExceptionInterface)
-- -- -- -- TooManyRedirectsException
-- -- -- -- BadResponseException
-- -- -- -- -- ClientException
-- -- -- -- -- ServerException

Copy link
Member

@gmponos gmponos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the traits.. because the functions are pretty simple.. besides that in general I agree that separating the exceptions is a necessary evil step that we need to apply towards 7.0 and supporting PSR-18

@davidgrayston
Copy link
Contributor Author

@gmponos I can remove use of traits if preferable?

I was also wondering if the following methods could be removed with a note added to UPGRADING.md?

  • GuzzleHttp\Exception\ConnectException::getResponse()
  • GuzzleHttp\Exception\ConnectException::hasResponse()

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR.
I agree with this change.

Could you please remove the Traits?
Could you also remove the hasResponse() and getResponse() from the ConnectException.

@Nyholm Nyholm added this to the 7.0.0 milestone Jan 18, 2020
@davidgrayston
Copy link
Contributor Author

Would you prefer the PHPDoc for getHandlerContext() be duplicated in each exception, or could this be moved into a handler context interface, with each implementation using inheritdoc?

@Nyholm
Copy link
Member

Nyholm commented Jan 18, 2020

You can duplicate it.
It is "just exceptions" and it is just twice.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@Nyholm Nyholm requested a review from gmponos January 19, 2020 08:33
Copy link
Member

@gmponos gmponos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update changelog as well.. In general I am happy with it.

@Nyholm
Copy link
Member

Nyholm commented Jan 19, 2020

Generally, change log updates should not be done in a PR. That will lead to merge conflicts.

@Nyholm Nyholm merged commit 6b77639 into guzzle:master Jan 19, 2020
@davidgrayston davidgrayston deleted the psr-18-network-exception branch January 20, 2020 00:15
@Tobion
Copy link
Member

Tobion commented Feb 16, 2020

With these changes, we should also change RequestException to make the response non-optional. The response was only optional for ConnectException. But now ConnectException does not extend RequestException anymore. All sub-classes of RequestException have a response now AFAIK.

@Tobion
Copy link
Member

Tobion commented Feb 16, 2020

Looks like TooManyRedirectsException does not have a response but should have. The response is just not forwarded from what I see.

@Nyholm
Copy link
Member

Nyholm commented May 23, 2020

Thank you @Tobion

@sergejostir
Copy link

sergejostir commented Nov 30, 2020

The docs haven't been updated and still display the old hiearchy: https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions

brentmullen added a commit to brentmullen/php-client that referenced this pull request Mar 12, 2021
As of Guzzle 7.0.0, ConnectException was moved under TransferException from RequestException.
Guzzle docs to not properly reflect this.

Guzzle change: guzzle/guzzle#2541

In the case of a ConnectException being thrown, this would cause another exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants