diff --git a/gbif_webservices.py b/gbif_webservices.py index 1f213fc..aa4cc6c 100644 --- a/gbif_webservices.py +++ b/gbif_webservices.py @@ -17,6 +17,8 @@ class ConnectionIssue(Exception): pass +class GBIFApiError(Exception): + pass def _finalize_filters(filters): fixed_filters = {'hasCoordinate': 'true', 'limit': RECORDS_PER_PAGE} @@ -31,7 +33,10 @@ def count_occurrences(filters): except requests.exceptions.ConnectionError: raise ConnectionIssue else: - resp = req.json() + try: + resp = req.json() + except ValueError: # When GBIF throws an error message, it's plain text (not JSON) + raise GBIFApiError(req.text) try: c = resp['count'] diff --git a/metadata.txt b/metadata.txt index 1b345d1..565d1e8 100644 --- a/metadata.txt +++ b/metadata.txt @@ -10,7 +10,7 @@ name=GBIF Occurrences qgisMinimumVersion=2.0 description=Retrieve data from GBIF webservices (occurences API) directly within QGIS. -version=0.1.2 +version=0.1.3 author=Nicolas NoƩ - Belgian Biodiversity Platform email=n.noe@biodiversity.be about=GBIF Occurrences is a QGIS plugin to directly download and import GBIF occurrence data from the application interface. @@ -21,6 +21,7 @@ about=GBIF Occurrences is a QGIS plugin to directly download and import GBIF occ # Uncomment the following line and add your changelog: changelog= + 0.1.3 - Better error messages when incorrect filters. 0.1.2 - Plugin remove itself from menu on deactivation 0.1.1 - Packaging fix for https://github.com/BelgianBiodiversityPlatform/qgis-gbif-api/issues/21 0.1 - Initial release diff --git a/qgis_occurrences_dialog.py b/qgis_occurrences_dialog.py index 6512455..ae8a360 100644 --- a/qgis_occurrences_dialog.py +++ b/qgis_occurrences_dialog.py @@ -20,7 +20,7 @@ from helpers import create_and_add_layer, add_gbif_occ_to_layer from gbif_webservices import (get_occurrences_in_baches, count_occurrences, ConnectionIssue, - MAX_TOTAL_RECORDS_GBIF) + GBIFApiError, MAX_TOTAL_RECORDS_GBIF) parent_dir = os.path.abspath(os.path.dirname(__file__)) vendor_dir = os.path.join(parent_dir, 'vendor') @@ -90,7 +90,8 @@ def __init__(self, parent=None): self.catalogNumberField, self.publishingCountryComboBox, self.institutionCodeField, self.collectionCodeField, self.yearRangeBox, self.maxYearEdit, self.minYearEdit, - self.taxonKeyField, self.datasetKeyField, self.recordedByField) + self.taxonKeyField, self.datasetKeyField, + self.recordedByField) self.loadButton.clicked.connect(self.load_occurrences) self.yearRangeBox.clicked.connect(self.year_range_ui) @@ -138,7 +139,9 @@ def show_progress(self, already_loaded_records, total_records): self.progressBar.setValue(percent) def connection_error_message(self): - msg = "Cannot connect to GBIF. Please check your Internet connection." + self.error_message("Cannot connect to GBIF. Please check your Internet connection.") + + def error_message(self, msg): QtGui.QMessageBox.critical(self, "Error", msg) def _ui_to_filters(self): @@ -167,6 +170,8 @@ def load_occurrences(self): count = count_occurrences(filters) except ConnectionIssue: self.connection_error_message() + except GBIFApiError as e: + self.error_message("GBIF Error: " + str(e)) else: if count > MAX_TOTAL_RECORDS_GBIF: self.dialog_too_many_results() diff --git a/test/test_qgis_occurrences_dialog.py b/test/test_qgis_occurrences_dialog.py index 9e17748..c3ceeb5 100644 --- a/test/test_qgis_occurrences_dialog.py +++ b/test/test_qgis_occurrences_dialog.py @@ -433,7 +433,10 @@ def test_attributes(self): self.assertIn("GEODETIC_DATUM_ASSUMED_WGS84", record_id_90129834.attribute('issues')) self.assertIn("COUNTRY_DERIVED_FROM_COORDINATES", record_id_90129834.attribute('issues')) - + # TODO: + # Regression test: test plugin remove itself from Menu when deactivated. + # Regression test: that GBIF API error messages are displayed back to the user + # (for example: using mammalia as TaxonKey => Invalid integer range: mammalia) if __name__ == "__main__": suite = unittest.makeSuite(GBIFOccurrencesDialogTest) runner = unittest.TextTestRunner(verbosity=2)