Skip to content

Commit c0f1513

Browse files
authored
Fix AttributeError: 'ClientConnectorCertificateError' object has no attribute '_os_error'. (#12136)
1 parent 830ae44 commit c0f1513

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
@@ -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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,21 @@ 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__(
346-
self, connection_key: ConnectionKey, certificate_error: Exception
348+
# TODO: If we require ssl in future, this can become ssl.CertificateError
349+
self,
350+
connection_key: ConnectionKey,
351+
certificate_error: Exception,
347352
) -> None:
348-
self._conn_key = connection_key
353+
if isinstance(certificate_error, cert_errors + (OSError,)):
354+
# ssl.CertificateError has errno and strerror, so we should be fine
355+
os_error = certificate_error
356+
else:
357+
os_error = OSError()
358+
359+
super().__init__(connection_key, os_error)
349360
self._certificate_error = certificate_error
350361
self.args = (connection_key, certificate_error)
351362

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)