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

Custom error message for Unix connect errors #4985

Merged
merged 3 commits into from
Nov 21, 2020
Merged

Conversation

zealws
Copy link
Contributor

@zealws zealws commented Sep 30, 2020

What do these changes do?

UnixConnector client errors result in an error message which could be improved.

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host host:80 ssl:default [Connection refused]

In particular, the error message notes the host/port pair, but for UnixConnector, the socket path is the primary way of identifying the connection, not host/port pair.

This is what the new error message looks like:

aiohttp.client_exceptions.UnixClientConnectorError: Cannot connect to unix socket /tmp/server.sock ssl:default [Connection refused]

Full error and sample code in this gist

Are there changes in behavior for the user?

UnixConnector now raises UnixClientConnectorError instead of ClientConnectorError.

UnixClientConnectorError is a subclass of ClientConnectorError so any catch ClientConnectorError code should still function as expected.

Related issue number

N/A

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Sep 30, 2020
@zealws
Copy link
Contributor Author

zealws commented Sep 30, 2020

This fixes #4984

@zealws
Copy link
Contributor Author

zealws commented Sep 30, 2020

I didn't feel like adding a unit-test was essential for this, but if you'd like me to add one, I can.

@codecov-commenter
Copy link

codecov-commenter commented Sep 30, 2020

Codecov Report

Merging #4985 into master will decrease coverage by 0.01%.
The diff coverage is 80.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4985      +/-   ##
==========================================
- Coverage   97.60%   97.58%   -0.02%     
==========================================
  Files          43       43              
  Lines        8932     8941       +9     
  Branches     1406     1406              
==========================================
+ Hits         8718     8725       +7     
- Misses         95       97       +2     
  Partials      119      119              
Impacted Files Coverage Δ
aiohttp/client_exceptions.py 98.31% <77.77%> (-1.69%) ⬇️
aiohttp/connector.py 96.28% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 90acab1...f466311. Read the comment docs.

super().__init__(connection_key, os_error)

@property
def path(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd prefer this to be a pathlib.Path instance that's only stringified when used.

Copy link
Member

Choose a reason for hiding this comment

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

UnixConnector historically works with str and has .path string property.
I think we should keep the type for consistency.

Raised in :class:`aiohttp.connector.UnixConnector` if
connection to unix socket can not be established.
"""
def __init__(self, path: str, connection_key: ConnectionKey,
Copy link
Member

Choose a reason for hiding this comment

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

It'd be great to expect os.PathLike / pathlib.Path for path.

return self._path

def __str__(self) -> str:
return ('Cannot connect to unix socket {0.path} ssl:{1} [{2}]'
Copy link
Member

Choose a reason for hiding this comment

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

I have two comments here:

  1. it's better to use named placeholders instead of positional ones
  2. you could do self.path in args like with other vars, otherwise, it looks inconsistent

Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

It'd be great to add some integration tests + check how this works with abstract sockets.

@zealws
Copy link
Contributor Author

zealws commented Oct 15, 2020

Thanks for the comments @webknjaz . I'll update this tomorrow per your suggestions.

I can add an integration test for a simple unix socket. However, I'm not sure exactly what you mean by "abstract sockets". Do you have some documentation on the topic you can refer me to?

@webknjaz
Copy link
Member

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Looks good in general.
@webknjaz please feel free to merge when ready.
I think the PR can be backported to 3.7 branch (label + bot works fine)

aiohttp/client_exceptions.py Outdated Show resolved Hide resolved
@CLAassistant

This comment has been minimized.

Co-authored-by: Sviatoslav Sydorenko <[email protected]>
class UnixClientConnectorError(ClientConnectorError):
"""Unix connector error.

Raised in :class:`aiohttp.connector.UnixConnector` if
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Raised in :class:`aiohttp.connector.UnixConnector` if
Raised in aiohttp.UnixConnector if

super().__init__(connection_key, os_error)

@property
def path(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

UnixConnector historically works with str and has .path string property.
I think we should keep the type for consistency.

@asvetlov
Copy link
Member

I'll fix the black formatting error by the next commit.

@asvetlov asvetlov merged commit e19c79e into aio-libs:master Nov 21, 2020
@github-actions
Copy link
Contributor

💔 Backport was not successful

The PR was attempted backported to the following branches:

  • ❌ 3.8: Commit could not be cherrypicked due to conflicts

asvetlov pushed a commit that referenced this pull request Nov 21, 2020
* Custom error message for Unix connect errors

* Update aiohttp/client_exceptions.py

Co-authored-by: Sviatoslav Sydorenko <[email protected]>

Co-authored-by: Andrew Svetlov <[email protected]>
Co-authored-by: Sviatoslav Sydorenko <[email protected]>.
(cherry picked from commit e19c79e)

Co-authored-by: Zeal Wierslee <[email protected]>
asvetlov added a commit that referenced this pull request Nov 21, 2020
* Custom error message for Unix connect errors

* Update aiohttp/client_exceptions.py

Co-authored-by: Sviatoslav Sydorenko <[email protected]>

Co-authored-by: Andrew Svetlov <[email protected]>
Co-authored-by: Sviatoslav Sydorenko <[email protected]>.
(cherry picked from commit e19c79e)

Co-authored-by: Zeal Wierslee <[email protected]>

Co-authored-by: Zeal Wierslee <[email protected]>
commonism pushed a commit to commonism/aiohttp that referenced this pull request Apr 27, 2021
* Custom error message for Unix connect errors

* Update aiohttp/client_exceptions.py

Co-authored-by: Sviatoslav Sydorenko <[email protected]>

Co-authored-by: Andrew Svetlov <[email protected]>
Co-authored-by: Sviatoslav Sydorenko <[email protected]>
commonism pushed a commit to commonism/aiohttp that referenced this pull request Apr 27, 2021
* Custom error message for Unix connect errors

* Update aiohttp/client_exceptions.py

Co-authored-by: Sviatoslav Sydorenko <[email protected]>

Co-authored-by: Andrew Svetlov <[email protected]>
Co-authored-by: Sviatoslav Sydorenko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants