Skip to content

add exception kwarg to various validation constructs - #732

Closed
Evidlo wants to merge 1 commit into
construct:masterfrom
Evidlo:exceptions
Closed

Evidlo wants to merge 1 commit into
construct:masterfrom
Evidlo:exceptions

Conversation

@Evidlo

@Evidlo Evidlo commented Jun 8, 2018

Copy link
Copy Markdown

Checksum, Check, Const, Validator, ExprValidator, OneOf, NoneOf

@arekbulski

Copy link
Copy Markdown
Member

I would likely reject this PR. I appreciate anyone taking effort to submit something, anything to the codebase but please understand that I dont like this idea of runtime-provided exception types. Expanding the set of error types, for each class, is fine. This is how Construct does it. But runtime exceptino types is meh. If you would like to go that way, please dont change this PR but create a new one.

@Evidlo

Evidlo commented Jun 21, 2018 •

Copy link
Copy Markdown
Author

Can I split the exceptions out into a separate _exception function so that the constructs can be subclassed and _exception overriden without rewriting all of _parse?

@Evidlo Evidlo closed this Jun 21, 2018
@arekbulski

Copy link
Copy Markdown
Member

No. Look at how different classes raise exceptions that are derived from central ContructError. You can just extend the exception tree.

@Evidlo

Evidlo commented Jun 21, 2018 •

Copy link
Copy Markdown
Author

I don't quite understand what you mean. Suppose I have this structure:

foo = Struct(
    "data1" / Bytes(8),
    "check1" / Checksum(Bytes(8), my_hash, this.data1),
    "data2" / Bytes(8),
    "check2" / Checksum(Bytes(8), my_hash, this.data2)
)

How do you catch an exception in check1 vs check2 with the method you propose?

@arekbulski

Copy link
Copy Markdown
Member

Ah right. You dont. There are different exceptions for different classes, but in current implementation there is no means of distinguishing individual fields in a struct. Now I see your point. It would be nice if you would suggest (and then implement) some solution to that, but what you proposed earlier, I just dont like it, its not consistent with existing design and its bloating the codebase.

@Evidlo

Evidlo commented Jun 21, 2018 •

Copy link
Copy Markdown
Author

There's really only 3 solutions with the current codebase:

  1. allow an exception kwarg for the classes that do checks.
  2. move the exception raising into an _exception function
  3. make the current path available in the raised exception

The first two are the same mentioned previously.

I don't think the third way is great because path isn't well documented and isn't available in certain places where exceptions are raised.

Solution 1 was what I proposed in #731 a few weeks ago

@arekbulski

Copy link
Copy Markdown
Member

I think I would prefer the third solution, if any.

@Evidlo

Evidlo commented Jun 21, 2018 •

Copy link
Copy Markdown
Author

In solution 3, ChecksumError, CheckError, ConstError and ValidationError would subclass from some exception that accepts a path argument, like this:

class ConstructError(Exception):
    pass
class ConstructErrorWithPath(ConstructError):
    def __init__(self, message, path=None):
        super(ConstructError, self).__init__(message)
        self.path = path
class ChecksumError(ConstructErrorWithPath):
    pass

try:
    path = '(parsing) -> foo -> bar'
    # checksum failed
    raise ChecksumError(
        "wrong checksum, read 12345, computed 67890",
        path=path
    )
except ChecksumError as e:
    if e.path == '(parsing) -> foo -> bar':
        # do something

@Evidlo

Evidlo commented Jun 23, 2018

Copy link
Copy Markdown
Author

I've been considering extending ConstructError like so:

class ConstructError(Exception):
    def __init__(self, message, path=None):
        self.path = path
        if path is None:
            super(ConstructError, self).__init__(message)
        else:
            message = "Error in path {}\n".format(path) + message
            super(ConstructError, self).__init__(message)

Exceptions would be much easier to debug because you would have the full path that the exception occured in.

I'd also like to add a path argument to stream_read, stream_read_entire, stream_write, stream_seek and stream_tell, so that the path information is available when a StreamError is raised.

StreamError: Error in path (parsing) -> data
stream read less than specified amount, expected 12884901892, found 74

@Evidlo

Evidlo commented Jun 27, 2018

Copy link
Copy Markdown
Author

Is this an acceptable solution?

@arekbulski arekbulski reopened this Jan 18, 2020
@arekbulski

arekbulski commented Jan 18, 2020 •

Copy link
Copy Markdown
Member

I like this 3rd path approach. Lets contemplate on this. Are you up to writing a PR?
As for this PR, I am not keen on accepting it. It diverges from what I want from Construct.

@arekbulski

arekbulski commented Jan 18, 2020 •

Copy link
Copy Markdown
Member

I'd also like to add a path argument to stream_read, stream_read_entire, stream_write, stream_seek and stream_tell, so that the path information is available when a StreamError is raised.

I am working on it ATM.

EDIT: I finished it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants