Skip to content

Commit 02b4f86

Browse files
DiddiLeijauranusjrpradyunsg
authored
Return a better error message if a file: URL is not found (#10263)
Co-authored-by: Tzu-ping Chung <[email protected]> Co-authored-by: Pradyun Gedam <[email protected]>
1 parent 04b9ece commit 02b4f86

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

news/10263.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Present a better error message, when a ``file:`` URL is not found.

src/pip/_internal/network/session.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import email.utils
6+
import io
67
import ipaddress
78
import json
89
import logging
@@ -207,8 +208,11 @@ def send(
207208
try:
208209
stats = os.stat(pathname)
209210
except OSError as exc:
211+
# format the exception raised as a io.BytesIO object,
212+
# to return a better error message:
210213
resp.status_code = 404
211-
resp.raw = exc
214+
resp.reason = type(exc).__name__
215+
resp.raw = io.BytesIO(f"{resp.reason}: {exc}".encode("utf8"))
212216
else:
213217
modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
214218
content_type = mimetypes.guess_type(pathname)[0] or "text/plain"

tests/functional/test_bad_url.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# test the error message returned by pip when
2+
# a bad "file:" URL is passed to it.
3+
4+
from typing import Any
5+
6+
7+
def test_filenotfound_error_message(script: Any) -> None:
8+
# Test the error message returned when using a bad 'file:' URL.
9+
# make pip to fail and get an error message
10+
# by running "pip install -r file:nonexistent_file"
11+
proc = script.pip("install", "-r", "file:unexistent_file", expect_error=True)
12+
assert proc.returncode == 1
13+
expect = (
14+
"ERROR: 404 Client Error: FileNotFoundError for url: file:///unexistent_file"
15+
)
16+
assert proc.stderr.rstrip() == expect

0 commit comments

Comments
 (0)