Skip to content

Commit e0bd413

Browse files
committed
Fix AttributeError: 'ClientConnectorCertificateError' object has no attribute '_os_error'.
ClientConnectorCertificateError is a subclass of ClientConnectorError and should have all of its attributes (LSP). However, when I try to access the os_error attribute, I get the mentioned exception.
1 parent 4ec897b commit e0bd413

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
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
@@ -392,6 +392,7 @@ Vladimir Kamarzin
392392
Vladimir Kozlovski
393393
Vladimir Rutsky
394394
Vladimir Shulyak
395+
Vladimir Vinogradenko
395396
Vladimir Zakharov
396397
Vladyslav Bohaichuk
397398
Vladyslav Bondar

aiohttp/client_exceptions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,18 @@ class ClientConnectorSSLError(*ssl_error_bases): # type: ignore[misc]
342342
class ClientConnectorCertificateError(*cert_errors_bases): # type: ignore[misc]
343343
"""Response certificate error."""
344344

345+
_conn_key: ConnectionKey
346+
345347
def __init__(
346348
self, connection_key: ConnectionKey, certificate_error: Exception
347349
) -> None:
348-
self._conn_key = connection_key
350+
if isinstance(certificate_error, cert_errors):
351+
# ssl.CertificateError has errno and strerror, so we should be fine
352+
os_error = certificate_error
353+
else:
354+
os_error = OSError() # type: ignore[misc]
355+
356+
super().__init__(connection_key, os_error)
349357
self._certificate_error = certificate_error
350358
self.args = (connection_key, certificate_error)
351359

tests/test_client_exceptions.py

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

204+
def test_oserror(self) -> None:
205+
certificate_error = OSError(1, "Bad certificate")
206+
err = client.ClientConnectorCertificateError(
207+
connection_key=self.connection_key, certificate_error=certificate_error
208+
)
209+
assert err.os_error == certificate_error
210+
assert err.errno == 1
211+
assert err.strerror == "Bad certificate"
212+
204213

205214
class TestServerDisconnectedError:
206215
def test_ctor(self) -> None:

0 commit comments

Comments
 (0)