From f4d174d5afbbd7eb28e8e22c141cc5094dc84246 Mon Sep 17 00:00:00 2001 From: Michael Moen Allport Date: Thu, 20 Jun 2024 11:33:47 +0200 Subject: [PATCH] Add better exception message when failing to create PseudoFieldRequest (#405) * Add better exception message when failing to create PseudoFieldRequest * add missing append --- pyproject.toml | 2 +- src/dapla_pseudo/utils.py | 79 ++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 222f896b..f0a7fc9c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dapla-toolbelt-pseudo" -version = "2.1.0" +version = "2.1.1" description = "Pseudonymization extensions for Dapla" authors = ["Dapla Developers "] license = "MIT" diff --git a/src/dapla_pseudo/utils.py b/src/dapla_pseudo/utils.py index c95847d1..c9460aea 100644 --- a/src/dapla_pseudo/utils.py +++ b/src/dapla_pseudo/utils.py @@ -13,6 +13,7 @@ from dapla import FileClient from gcsfs.core import GCSFile from google.auth.exceptions import DefaultCredentialsError +from pydantic import ValidationError from dapla_pseudo.constants import PseudoOperation from dapla_pseudo.exceptions import FileInvalidError @@ -98,45 +99,63 @@ def build_pseudo_field_request( """Builds a FieldRequest object.""" mutable_df.match_rules(rules, target_rules) matched_fields = mutable_df.get_matched_fields() + requests: list[PseudoFieldRequest | DepseudoFieldRequest | RepseudoFieldRequest] = ( + [] + ) + req: PseudoFieldRequest | DepseudoFieldRequest | RepseudoFieldRequest match pseudo_operation: case PseudoOperation.PSEUDONYMIZE: - return [ - PseudoFieldRequest( - pseudo_func=field.func, - name=field.path, - pattern=field.pattern, - values=field.get_value(), - keyset=KeyWrapper(custom_keyset).keyset, - ) - for field in matched_fields.values() - ] + for field in matched_fields.values(): + try: + req = PseudoFieldRequest( + pseudo_func=field.func, + name=field.path, + pattern=field.pattern, + values=field.get_value(), + keyset=KeyWrapper(custom_keyset).keyset, + ) + requests.append(req) + except ValidationError as e: + raise Exception( + f"Path: {field.path}, Values: {field.get_value()}" + ) from e case PseudoOperation.DEPSEUDONYMIZE: - return [ - DepseudoFieldRequest( - pseudo_func=field.func, - name=field.path, - pattern=field.pattern, - values=field.get_value(), - keyset=KeyWrapper(custom_keyset).keyset, - ) - for field in matched_fields.values() - ] - case PseudoOperation.REPSEUDONYMIZE: - if target_rules is not None: - return [ - RepseudoFieldRequest( - source_pseudo_func=field.func, - target_pseudo_func=field.target_func, + for field in matched_fields.values(): + try: + req = DepseudoFieldRequest( + pseudo_func=field.func, name=field.path, pattern=field.pattern, values=field.get_value(), - source_keyset=KeyWrapper(custom_keyset).keyset, - target_keyset=KeyWrapper(target_custom_keyset).keyset, + keyset=KeyWrapper(custom_keyset).keyset, ) - for field in matched_fields.values() - ] + requests.append(req) + except ValidationError as e: + raise Exception( + f"Path: {field.path}, Values: {field.get_value()}" + ) from e + + case PseudoOperation.REPSEUDONYMIZE: + if target_rules is not None: + for field in matched_fields.values(): + try: + req = RepseudoFieldRequest( + source_pseudo_func=field.func, + target_pseudo_func=field.target_func, + name=field.path, + pattern=field.pattern, + values=field.get_value(), + source_keyset=KeyWrapper(custom_keyset).keyset, + target_keyset=KeyWrapper(target_custom_keyset).keyset, + ) + requests.append(req) + except ValidationError as e: + raise Exception( + f"Path: {field.path}, Values: {field.get_value()}" + ) from e else: raise ValueError("Found no target rules") + return requests def build_pseudo_file_request(