|
16 | 16 | from jsonpath_ng.ext import parse |
17 | 17 |
|
18 | 18 | from aws_lambda_powertools.utilities.data_masking.exceptions import ( |
| 19 | + DataMaskingError, |
19 | 20 | DataMaskingFieldNotFoundError, |
20 | 21 | DataMaskingUnsupportedTypeError, |
21 | 22 | ) |
22 | 23 | from aws_lambda_powertools.utilities.data_masking.provider import BaseProvider |
23 | | -from aws_lambda_powertools.warnings import PowertoolsUserWarning |
24 | 24 |
|
25 | 25 | if TYPE_CHECKING: |
26 | 26 | from collections.abc import Callable, Mapping, Sequence |
@@ -395,10 +395,10 @@ def _apply_action_to_fields( |
395 | 395 | if not result_parse: |
396 | 396 | if self.raise_on_missing_field: |
397 | 397 | # If the data for the field is not found, raise an exception. |
398 | | - raise DataMaskingFieldNotFoundError(f"Field or expression {field_parse} not found in {data_parsed}") |
| 398 | + raise DataMaskingFieldNotFoundError(f"Field or expression {field_parse} not found") |
399 | 399 | else: |
400 | 400 | # If the data for the field is not found, warning. |
401 | | - warnings.warn(f"Field or expression {field_parse} not found in {data_parsed}", stacklevel=2) |
| 401 | + warnings.warn(f"Field or expression {field_parse} not found", stacklevel=2) |
402 | 402 |
|
403 | 403 | # For in-place updates, json_parse accepts a callback function |
404 | 404 | # that receives 3 args: field_value, fields, field_name |
@@ -427,31 +427,30 @@ def _apply_masking_rules(self, data: dict, masking_rules: dict) -> dict: |
427 | 427 | for path, rule in masking_rules.items(): |
428 | 428 | try: |
429 | 429 | jsonpath_expr = parse(f"$.{path}") |
430 | | - matches = jsonpath_expr.find(result) |
| 430 | + except Exception as exc: |
| 431 | + raise DataMaskingError(f"Invalid masking path: {path}") from exc |
431 | 432 |
|
432 | | - if not matches: |
433 | | - warnings.warn(f"No matches found for path: {path}", stacklevel=2) |
434 | | - continue |
| 433 | + matches = jsonpath_expr.find(result) |
| 434 | + if not matches: |
| 435 | + if self.raise_on_missing_field: |
| 436 | + raise DataMaskingFieldNotFoundError(f"Field or expression {path} not found") |
435 | 437 |
|
436 | | - for match in matches: |
437 | | - try: |
438 | | - value = match.value |
439 | | - if value is not None: |
440 | | - masked_value = self.provider.erase(str(value), **rule) |
441 | | - match.full_path.update(result, masked_value) |
442 | | - |
443 | | - except Exception as e: |
444 | | - warnings.warn( |
445 | | - f"Error masking value for path {path}: {str(e)}", |
446 | | - category=PowertoolsUserWarning, |
447 | | - stacklevel=2, |
448 | | - ) |
449 | | - continue |
450 | | - |
451 | | - except Exception as e: |
452 | | - warnings.warn(f"Error processing path {path}: {str(e)}", category=PowertoolsUserWarning, stacklevel=2) |
| 438 | + warnings.warn(f"No matches found for path: {path}", stacklevel=2) |
453 | 439 | continue |
454 | 440 |
|
| 441 | + for match in matches: |
| 442 | + value = match.value |
| 443 | + if value is None: |
| 444 | + continue |
| 445 | + |
| 446 | + try: |
| 447 | + masked_value = self.provider.erase(str(value), **rule) |
| 448 | + match.full_path.update(result, masked_value) |
| 449 | + except DataMaskingError: |
| 450 | + raise |
| 451 | + except Exception as exc: |
| 452 | + raise DataMaskingError(f"Failed to mask field at path: {path}") from exc |
| 453 | + |
455 | 454 | return result |
456 | 455 |
|
457 | 456 | def _mask_nested_field(self, data: dict, field_path: str, mask_function): |
|
0 commit comments