Skip to content

Commit

Permalink
Return a better error message if a file: URL is not found (pypa#10263)
Browse files Browse the repository at this point in the history
Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
Co-authored-by: Pradyun Gedam <pradyunsg@gmail.com>
  • Loading branch information
3 people authored Oct 8, 2021
1 parent 04b9ece commit 02b4f86
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/10263.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Present a better error message, when a ``file:`` URL is not found.
6 changes: 5 additions & 1 deletion src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import email.utils
import io
import ipaddress
import json
import logging
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/test_bad_url.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 02b4f86

Please sign in to comment.