Skip to content

Commit 5d922ee

Browse files
[PR #12136/c0f1513c backport][3.13] Fix AttributeError: 'ClientConnectorCertificateError' object has no attribute '_os_error'. (#12147)
**This is a backport of PR #12136 as merged into master (c0f1513).** Co-authored-by: themylogin <themylogin@gmail.com>
1 parent 57ac25b commit 5d922ee

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGES/12136.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``ClientConnectorCertificateError.os_error`` no longer raises :exc:`AttributeError`
2+
-- by :user:`themylogin`.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ Vladimir Kamarzin
378378
Vladimir Kozlovski
379379
Vladimir Rutsky
380380
Vladimir Shulyak
381+
Vladimir Vinogradenko
381382
Vladimir Zakharov
382383
Vladyslav Bohaichuk
383384
Vladyslav Bondar

aiohttp/client_exceptions.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,21 @@ class ClientConnectorSSLError(*ssl_error_bases): # type: ignore[misc]
386386
class ClientConnectorCertificateError(*cert_errors_bases): # type: ignore[misc]
387387
"""Response certificate error."""
388388

389+
_conn_key: ConnectionKey
390+
389391
def __init__(
390-
self, connection_key: ConnectionKey, certificate_error: Exception
392+
# TODO: If we require ssl in future, this can become ssl.CertificateError
393+
self,
394+
connection_key: ConnectionKey,
395+
certificate_error: Exception,
391396
) -> None:
392-
self._conn_key = connection_key
397+
if isinstance(certificate_error, cert_errors + (OSError,)):
398+
# ssl.CertificateError has errno and strerror, so we should be fine
399+
os_error = certificate_error
400+
else:
401+
os_error = OSError()
402+
403+
super().__init__(connection_key, os_error)
393404
self._certificate_error = certificate_error
394405
self.args = (connection_key, certificate_error)
395406

tests/test_client_exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ def test_str(self) -> None:
232232
" [Exception: ('Bad certificate',)]"
233233
)
234234

235+
def test_oserror(self) -> None:
236+
certificate_error = OSError(1, "Bad certificate")
237+
err = client.ClientConnectorCertificateError(
238+
connection_key=self.connection_key, certificate_error=certificate_error
239+
)
240+
assert err.os_error == certificate_error
241+
assert err.errno == 1
242+
assert err.strerror == "Bad certificate"
243+
235244

236245
class TestServerDisconnectedError:
237246
def test_ctor(self) -> None:

0 commit comments

Comments
 (0)