I'm trying to catch an error thrown by the prodigy Python SDK in my code (when deleting a dataset that doesn't exist with Database.drop_dataset(), and I see that it raises a DatasetDoesNotExist error, but it's unclear to me how to import that into my code. Through my IDE I see that there is a prodigy.errors module, but it seems I'm not allowed to import from there.
I tried looking in the docs, but no reference to that error to be found.
You should be able to import the top level error class i.e. ProdigyError:
from prodigy.errors import ProdigyError
Does this import statement not work for you? ProdigyError has title, textand msg attributes so you should get to a meaningful msg like so:
try:
db.drop_dataset("my_dataset")
except ProdigyError as e:
print(e.msg)
# Dataset: 'my_dataset' not found in the currently configured Prodigy Database: sqlite
I admit the error handling is not documented enough. Definitely on a todo list now!
Got it! I'm now doing this to make sure I'm handling the right error. I'd rather import the error itself, tbh, but this will do for now:
def delete_dataset(self, **kwargs: Any) -> None:
"""Deletes a dataset from the annotation interface.
Args:
**kwargs: Additional keyword arguments to pass to the Prodigy
client.
Raises:
ValueError: If the dataset name is not provided or if the dataset
does not exist.
"""
db = self._get_db()
if not (dataset_name := kwargs.get("dataset_name")):
raise ValueError("`dataset_name` keyword argument is required.")
try:
db.drop_dataset(name=dataset_name)
except ProdigyError as e:
# see https://support.prodi.gy/t/how-to-import-datasetdoesnotexist-error/7205
if type(e).__name__ == "DatasetNotFound":
raise ValueError(
f"Dataset name '{dataset_name}' does not exist."
) from e