Handle Error for Invalid QGIS Hub API Responses#137
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR improves error handling for QGIS Hub API connectivity issues by replacing raw exceptions with user-friendly error messages.
Key changes:
- Wraps JSON decode errors and download errors in a custom
DownloadErrorexception with a consistent user-friendly message - Adds exception handling in the GUI layer to gracefully display errors to users
- Updates test cases to verify the new error handling behavior
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| qgis_hub_plugin/core/api_client.py | Refactors error handling to wrap JSON decode and download errors with user-friendly messages, extracts JSON loading logic into _load_response helper function |
| qgis_hub_plugin/gui/resource_browser.py | Adds try-except block to catch DownloadError and display user-friendly error message in the UI |
| tests/qgis/test_api_client_mocked.py | Updates test to verify DownloadError is raised with correct message instead of raw JSONDecodeError, adds new test for download error handling |
Comments suppressed due to low confidence (1)
qgis_hub_plugin/gui/resource_browser.py:243
- This check for
response is Noneis now unreachable. The refactoredget_all_resourcesfunction inapi_client.pyalways raisesDownloadErrorinstead of returningNonewhen it fails (line 41 of api_client.py). This code path can never execute and should be removed.
if response is None:
self.show_warning_message("Error populating the resources")
return
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
qgis_hub_plugin/core/api_client.py
Outdated
There was a problem hiding this comment.
The f-strings here are unnecessary since there are no variable interpolations. The BASE_URL is only included as literal text in parentheses. Consider using a regular string or a simple concatenation: API_UNAVAILABLE_MESSAGE = \"Unable to reach the QGIS Hub API. Please check the QGIS Hub API status and try again. (\" + BASE_URL + \")\"
| f"Unable to reach the QGIS Hub API. " | |
| f"Please check the QGIS Hub API status and try again. ({BASE_URL})" | |
| "Unable to reach the QGIS Hub API. " | |
| "Please check the QGIS Hub API status and try again. (" + BASE_URL + ")" |
|
Can you make the link clickable? So that user can try to cilck it directly and see if there is an error in the server. |
|
The link should be clickable now. |
|
Is it ready to be reviewed @ronitjadhav ? |
|
Yes it is ready 👍🏼 |
Description
This PR addresses the issue of handling invalid responses from the QGIS Hub API. It ensures the application gracefully deals with unexpected, missing, or corrupted data so the UI no longer exposes stack traces. The API client now raises a consistent, user-friendly error that identifies the failing base URL, the resource browser surfaces that message, and new unit tests cover both corrupted cache files and download failures to guard against regressions.
Screenshot
fix #94