Skip to content

grass.utils: Fix the download error message and limit the URL schemes - #7687

Merged
wenzeslaus merged 7 commits into
OSGeo:mainfrom
wenzeslaus:fix-download-error-messages
Jul 15, 2026
Merged

wenzeslaus merged 7 commits into
OSGeo:mainfrom
wenzeslaus:fix-download-error-messages

Conversation

@wenzeslaus

@wenzeslaus wenzeslaus commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

A failed download was reported with a message copied from g.extension.all which lost its {desc} placeholder, so it ended with a dangling comma and was formatted with the whole HTTPError object instead of a status code:

Download file from <URL>, return status code HTTP Error 404: Not Found,

It now passes the status code and its description, which restores the message as it is used elsewhere, so the msgid and its translations stay valid:

Download file from <URL>, return status code 404, Not Found

The identical error handling of the ZIP and TAR branches moves to a _download_file() function and the original exception is chained.

The URL scheme is now checked against the supported ones (http, https, ftp, file), because urlretrieve opens any scheme it is given (Bandit B310). Every scheme which works today keeps working. An archive on the local disk works only as file:// (a plain path failed with an unhandled ValueError), so the g.download.project documentation now says that.

The module had no tests. The new ones extract a ZIP and a TAR archive from the local disk and check that an unsupported scheme is rejected.

Note: this touches the same lines as #4032, so whichever is merged second needs a trivial conflict resolution.

Commit message

Fix the HTTP error message, which lost its {desc} placeholder and was
formatted with the whole HTTPError instead of a status code. Limit a download
to the supported schemes (http, https, ftp, file), since urlretrieve opens
any scheme, including data and custom ones (Bandit B310), and document that a
local archive needs the file scheme (no change in behavior, just doc clarity).
Skip the content type check for a local file, which fixed extracting a local ZIP
on Windows. Add tests for the module.

The message is a copy of the one in g.extension.all, but it lost the {desc}
placeholder, so it ended with a dangling comma, and it was formatted with the
whole HTTPError object instead of a status code:

    Download file from <URL>, return status code HTTP Error 404: Not Found,

Pass the status code and its description, which restores the message as it is
used elsewhere and keeps its existing translations:

    Download file from <URL>, return status code 404, Not Found

The ZIP and TAR branches handled the errors of the download itself in the same
way, so move the download to a function. Chain the original exception to keep
its details available.
@github-actions github-actions Bot added Python Related code is in Python libraries labels Jul 13, 2026
Comment thread python/grass/utils/download.py Fixed
echoix
echoix previously approved these changes Jul 13, 2026
urlretrieve opens whatever scheme it is given, including the data scheme and
any scheme added by a custom URL opener. Check the scheme against the ones
which are supported before the download.

A path without a scheme is not a URL and failed with an unhandled ValueError
from urlretrieve, and a Windows path was reported as a failed connection.
Both are now reported as an unsupported URL. An archive on the local disk
works only with the file scheme, so say that in the documentation of
g.download.project instead of just mentioning a local file.
Cover the extraction of a ZIP and a TAR archive from the local disk and the
rejection of an unsupported URL scheme. There were no tests for the module.
@github-actions github-actions Bot added HTML Related code is in HTML module docs markdown Related to markdown, markdown files general tests Related to Test Suite labels Jul 13, 2026
@wenzeslaus wenzeslaus changed the title grass.utils: Fix the HTTP error message when a download fails grass.utils: Fix the download error message and limit the URL schemes Jul 13, 2026
@wenzeslaus
wenzeslaus requested a review from cwhite911 July 14, 2026 16:27
cwhite911
cwhite911 previously approved these changes Jul 14, 2026

@cwhite911 cwhite911 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

echoix
echoix previously approved these changes Jul 14, 2026
@echoix

echoix commented Jul 14, 2026

Copy link
Copy Markdown
Member

It seems on windows there isn’t a zip file for the test? Or the zip file isn’t an archive? Was it written as a text file instead of bytes, such as the zip contents pass through the encoding mechanisms?

For a file URL there is no server response, so urlretrieve synthesizes the
content type from the file name. On Windows the registry maps .zip to
application/x-zip-compressed, which the check does not accept, so extracting
a local ZIP failed there. Skip the check for the file scheme; a local file
which is not a ZIP is still caught by extract_zip.
@wenzeslaus
wenzeslaus dismissed stale reviews from echoix and cwhite911 via 01f8363 July 15, 2026 08:12

@echoix echoix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming tests passes

@wenzeslaus
wenzeslaus enabled auto-merge (squash) July 15, 2026 09:30
@wenzeslaus
wenzeslaus merged commit f131b6c into OSGeo:main Jul 15, 2026
26 checks passed
@wenzeslaus
wenzeslaus deleted the fix-download-error-messages branch July 15, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs general HTML Related code is in HTML libraries markdown Related to markdown, markdown files module Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants