From 32c093a55f757656143de5e3b6948915ed1d524f Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Fri, 22 Mar 2024 15:21:45 +0100 Subject: [PATCH] enrich raised errors with context message --- dsp_permissions_scripts/ap/ap_delete.py | 7 ++++++- dsp_permissions_scripts/ap/ap_get.py | 7 ++++++- dsp_permissions_scripts/ap/ap_set.py | 6 +++++- dsp_permissions_scripts/doap/doap_get.py | 7 ++++++- dsp_permissions_scripts/doap/doap_set.py | 6 +++++- dsp_permissions_scripts/oap/oap_get.py | 12 ++++++++++-- dsp_permissions_scripts/utils/project.py | 19 ++++++++++++++++--- 7 files changed, 54 insertions(+), 10 deletions(-) diff --git a/dsp_permissions_scripts/ap/ap_delete.py b/dsp_permissions_scripts/ap/ap_delete.py index c0f319d5..12eca5eb 100644 --- a/dsp_permissions_scripts/ap/ap_delete.py +++ b/dsp_permissions_scripts/ap/ap_delete.py @@ -2,6 +2,7 @@ from urllib.parse import quote_plus from dsp_permissions_scripts.ap.ap_model import Ap +from dsp_permissions_scripts.models.api_error import ApiError from dsp_permissions_scripts.utils.get_logger import get_logger from dsp_permissions_scripts.utils import connection @@ -10,7 +11,11 @@ def _delete_ap_on_server(ap: Ap) -> None: ap_iri = quote_plus(ap.iri, safe="") - connection.con.delete(f"/admin/permissions/{ap_iri}") + try: + connection.con.delete(f"/admin/permissions/{ap_iri}") + except ApiError as err: + err.message = f"Could not delete Administrative Permission {ap.iri}" + raise err from None def delete_ap_of_group_on_server( diff --git a/dsp_permissions_scripts/ap/ap_get.py b/dsp_permissions_scripts/ap/ap_get.py index 0129a8b8..99104e77 100644 --- a/dsp_permissions_scripts/ap/ap_get.py +++ b/dsp_permissions_scripts/ap/ap_get.py @@ -3,6 +3,7 @@ from dsp_permissions_scripts.ap.ap_model import Ap, ApValue +from dsp_permissions_scripts.models.api_error import ApiError from dsp_permissions_scripts.utils.get_logger import get_logger from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode from dsp_permissions_scripts.utils import connection @@ -37,7 +38,11 @@ def create_admin_route_object_from_ap(ap: Ap) -> dict[str, Any]: def _get_all_aps_of_project(project_iri: str) -> list[Ap]: project_iri = quote_plus(project_iri, safe="") - response = connection.con.get(f"/admin/permissions/ap/{project_iri}") + try: + response = connection.con.get(f"/admin/permissions/ap/{project_iri}") + except ApiError as err: + err.message = f"Could not get APs of project {project_iri}" + raise err from None aps: list[dict[str, Any]] = response["administrative_permissions"] ap_objects = [create_ap_from_admin_route_object(ap) for ap in aps] return ap_objects diff --git a/dsp_permissions_scripts/ap/ap_set.py b/dsp_permissions_scripts/ap/ap_set.py index 9e7ce74f..38d468d7 100644 --- a/dsp_permissions_scripts/ap/ap_set.py +++ b/dsp_permissions_scripts/ap/ap_set.py @@ -17,7 +17,11 @@ def _update_ap_on_server(ap: Ap) -> Ap: iri = quote_plus(ap.iri, safe="") payload = {"hasPermissions": create_admin_route_object_from_ap(ap)["hasPermissions"]} - response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload) + try: + response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload) + except ApiError as err: + err.message = f"Could not update Administrative Permission {ap.iri}" + raise err from None ap_updated: dict[str, Any] = response["administrative_permission"] ap_object_updated = create_ap_from_admin_route_object(ap_updated) return ap_object_updated diff --git a/dsp_permissions_scripts/doap/doap_get.py b/dsp_permissions_scripts/doap/doap_get.py index 80399f8b..54135bf5 100644 --- a/dsp_permissions_scripts/doap/doap_get.py +++ b/dsp_permissions_scripts/doap/doap_get.py @@ -2,6 +2,7 @@ from urllib.parse import quote_plus from dsp_permissions_scripts.doap.doap_model import Doap, DoapTarget, DoapTargetType +from dsp_permissions_scripts.models.api_error import ApiError from dsp_permissions_scripts.utils.get_logger import get_logger from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode from dsp_permissions_scripts.utils.scope_serialization import ( @@ -34,7 +35,11 @@ def _filter_doaps_by_target( def _get_all_doaps_of_project(project_iri: str) -> list[Doap]: project_iri = quote_plus(project_iri, safe="") - response = connection.con.get(f"/admin/permissions/doap/{project_iri}") + try: + response = connection.con.get(f"/admin/permissions/doap/{project_iri}") + except ApiError as err: + err.message = f"Error while getting DOAPs of project {project_iri}" + raise err from None doaps: list[dict[str, Any]] = response["default_object_access_permissions"] doap_objects = [create_doap_from_admin_route_response(doap) for doap in doaps] return doap_objects diff --git a/dsp_permissions_scripts/doap/doap_set.py b/dsp_permissions_scripts/doap/doap_set.py index d2a9d363..ce0b46ce 100644 --- a/dsp_permissions_scripts/doap/doap_set.py +++ b/dsp_permissions_scripts/doap/doap_set.py @@ -17,7 +17,11 @@ def _update_doap_scope_on_server(doap_iri: str, scope: PermissionScope) -> Doap: iri = quote_plus(doap_iri, safe="") payload = {"hasPermissions": create_admin_route_object_from_scope(scope)} - response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload) + try: + response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload) + except ApiError as err: + err.message = f"Could not update scope of DOAP {doap_iri}" + raise err from None new_doap = create_doap_from_admin_route_response(response["default_object_access_permission"]) return new_doap diff --git a/dsp_permissions_scripts/oap/oap_get.py b/dsp_permissions_scripts/oap/oap_get.py index 32682903..2830582f 100644 --- a/dsp_permissions_scripts/oap/oap_get.py +++ b/dsp_permissions_scripts/oap/oap_get.py @@ -55,7 +55,11 @@ def _get_next_page( This means that the page must be incremented until the response contains 0 or 1 resource. """ route = f"/v2/resources?resourceClass={quote_plus(resclass_iri)}&page={page}" - result = connection.con.get(route, headers=headers) + try: + result = connection.con.get(route, headers=headers) + except ApiError as err: + err.message = "Could not get next page" + raise err from None # result contains several resources: return them, then continue with next page if "@graph" in result: @@ -77,7 +81,11 @@ def _get_next_page( def get_resource(resource_iri: str) -> dict[str, Any]: """Requests the resource with the given IRI from DSP-API""" iri = quote_plus(resource_iri, safe="") - return connection.con.get(f"/v2/resources/{iri}") + try: + return connection.con.get(f"/v2/resources/{iri}") + except ApiError as err: + err.message = f"Error while getting resource {resource_iri}" + raise err from None def get_oap_by_resource_iri(resource_iri: str) -> Oap: diff --git a/dsp_permissions_scripts/utils/project.py b/dsp_permissions_scripts/utils/project.py index 22f2bf94..92053902 100644 --- a/dsp_permissions_scripts/utils/project.py +++ b/dsp_permissions_scripts/utils/project.py @@ -1,5 +1,6 @@ from urllib.parse import quote_plus +from dsp_permissions_scripts.models.api_error import ApiError from dsp_permissions_scripts.utils.get_logger import get_logger from dsp_permissions_scripts.utils.helpers import dereference_prefix from dsp_permissions_scripts.utils import connection @@ -8,14 +9,22 @@ def _get_onto_iris_of_project(project_iri: str) -> list[str]: - response = connection.con.get("/v2/ontologies/metadata") + try: + response = connection.con.get("/v2/ontologies/metadata") + except ApiError as err: + err.message = f"Could not get onto IRIs of project {project_iri}" + raise err from None all_ontologies = response["@graph"] project_onto_iris = [o["@id"] for o in all_ontologies if o["knora-api:attachedToProject"]["@id"] == project_iri] return project_onto_iris def _get_class_iris_of_onto(onto_iri: str) -> list[str]: - response = connection.con.get(f"/v2/ontologies/allentities/{quote_plus(onto_iri)}") + try: + response = connection.con.get(f"/v2/ontologies/allentities/{quote_plus(onto_iri)}") + except ApiError as err: + err.message = f"Could not get class IRIs of onto {onto_iri}" + raise err from None all_entities = response["@graph"] context = response["@context"] class_ids = [c["@id"] for c in all_entities if c.get("knora-api:isResourceClass")] @@ -35,6 +44,10 @@ def get_all_resource_class_iris_of_project(project_iri: str) -> list[str]: def get_project_iri_by_shortcode(shortcode: str) -> str: - response = connection.con.get(f"/admin/projects/shortcode/{shortcode}") + try: + response = connection.con.get(f"/admin/projects/shortcode/{shortcode}") + except ApiError as err: + err.message = f"Could not get project IRI by shortcode {shortcode}" + raise err from None iri: str = response["project"]["id"] return iri