Skip to content

Nondiscriminatory error handling in raiseorpush  #6

@tomjanus

Description

@tomjanus

Nondiscriminatory error handling in the raiseorpush context manager masks errors not related to parsing and validation.

For example, in parsers/pywrjsonparser.py changing lines 141-143 from the original code:

with component_exc_capture("metadata") as cc:
            self.metadata = PywrMetadata(self.src["metadata"])
            cc.capture_warnings(self.metadata)

to

with component_exc_capture("metadata") as cc:
            self.metadata = PywrMetadata(self.src["metadata"])
            cc.capture_warnings("some other variable")

will not generate an exception.

To fix, update the __exit__ dunder method in lines 34-42 in utils .py to:

    def __exit__(self, exc_type, exc_obj, exc_tb):
        if isinstance(exc_obj, WNTREPANETTypeValidationErrorBundle):
            for error in exc_obj.errors:
                #  Raise on warning implies raise on error
                if self.raise_warning or self.raise_error:
                    raise error from None
                self.dest.errors[self.component].append(error)
        elif exc_obj is not None:
            raise(exc_obj) from exc_obj
        return not self.raise_warning

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions