diff --git a/news/10263.bugfix.rst b/news/10263.bugfix.rst new file mode 100644 index 00000000000..83e29f6bc0a --- /dev/null +++ b/news/10263.bugfix.rst @@ -0,0 +1 @@ +Present a better error message, when a ``file:`` URL is not found. diff --git a/src/pip/_internal/network/session.py b/src/pip/_internal/network/session.py index e2916ca8198..cbe743ba6a1 100644 --- a/src/pip/_internal/network/session.py +++ b/src/pip/_internal/network/session.py @@ -3,6 +3,7 @@ """ import email.utils +import io import ipaddress import json import logging @@ -207,8 +208,11 @@ def send( try: stats = os.stat(pathname) except OSError as exc: + # format the exception raised as a io.BytesIO object, + # to return a better error message: resp.status_code = 404 - resp.raw = exc + resp.reason = type(exc).__name__ + resp.raw = io.BytesIO(f"{resp.reason}: {exc}".encode("utf8")) else: modified = email.utils.formatdate(stats.st_mtime, usegmt=True) content_type = mimetypes.guess_type(pathname)[0] or "text/plain" diff --git a/tests/functional/test_bad_url.py b/tests/functional/test_bad_url.py new file mode 100644 index 00000000000..bc3a987e6f2 --- /dev/null +++ b/tests/functional/test_bad_url.py @@ -0,0 +1,16 @@ +# test the error message returned by pip when +# a bad "file:" URL is passed to it. + +from typing import Any + + +def test_filenotfound_error_message(script: Any) -> None: + # Test the error message returned when using a bad 'file:' URL. + # make pip to fail and get an error message + # by running "pip install -r file:nonexistent_file" + proc = script.pip("install", "-r", "file:unexistent_file", expect_error=True) + assert proc.returncode == 1 + expect = ( + "ERROR: 404 Client Error: FileNotFoundError for url: file:///unexistent_file" + ) + assert proc.stderr.rstrip() == expect