Skip to content

Commit 89ccbe6

Browse files
author
Muhamed Fazal PS
committed
fix: raise FileNotFoundError for missing TLS material (fixes #7564)
When a TLS certificate or key file is not found, the code currently raises a generic OSError. This changes it to raise FileNotFoundError with the proper errno (ENOENT) and filename attribute, which is more specific and allows callers to catch this specific case. FileNotFoundError is a subclass of OSError, so this is backward compatible with existing code that catches OSError.
1 parent f361ead commit 89ccbe6

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/requests/adapters.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from __future__ import annotations
1010

11+
import errno
1112
import os.path
1213
import socket # noqa: F401
1314
import typing
@@ -353,13 +354,17 @@ def cert_verify(
353354
conn.cert_file = cert
354355
conn.key_file = None
355356
if conn.cert_file and not os.path.exists(conn.cert_file):
356-
raise OSError(
357+
raise FileNotFoundError(
358+
os.strerror(errno.ENOENT),
357359
f"Could not find the TLS certificate file, "
358-
f"invalid path: {conn.cert_file}"
360+
f"invalid path: {conn.cert_file}",
361+
conn.cert_file,
359362
)
360363
if conn.key_file and not os.path.exists(conn.key_file):
361-
raise OSError(
362-
f"Could not find the TLS key file, invalid path: {conn.key_file}"
364+
raise FileNotFoundError(
365+
os.strerror(errno.ENOENT),
366+
f"Could not find the TLS key file, invalid path: {conn.key_file}",
367+
conn.key_file,
363368
)
364369

365370
def build_response(self, req: PreparedRequest, resp: Any) -> Response:

0 commit comments

Comments
 (0)