Skip to content

Commit

Permalink
GBIF API Errors are now properly displayed to the user. See #27.
Browse files Browse the repository at this point in the history
  • Loading branch information
niconoe committed Feb 4, 2016
1 parent 5f76bf0 commit 77a2984
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
7 changes: 6 additions & 1 deletion gbif_webservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class ConnectionIssue(Exception):
pass

class GBIFApiError(Exception):
pass

def _finalize_filters(filters):
fixed_filters = {'hasCoordinate': 'true', 'limit': RECORDS_PER_PAGE}
Expand All @@ -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']
Expand Down
3 changes: 2 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]
about=GBIF Occurrences is a QGIS plugin to directly download and import GBIF occurrence data from the application interface.
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions qgis_occurrences_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion test/test_qgis_occurrences_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 77a2984

Please sign in to comment.