Skip to content

show path on exceptions during parsing/building - #826

Merged
arekbulski merged 1 commit into
construct:masterfrom
Evidlo:exception_path
Jan 19, 2020
Merged

arekbulski merged 1 commit into
construct:masterfrom
Evidlo:exception_path

Conversation

@Evidlo

@Evidlo Evidlo commented Jan 19, 2020 •

Copy link
Copy Markdown

Previous discussion: #732

I modified ConstructError to take an optional path kwarg.
I also modified all raised exceptions where path is in scope in core.py.

Sample output:

from construct import Struct, Bytes

x = Struct(
    'foo' / Bytes(1),
    'a' / Struct(
        'foo' / Bytes(1),
        'b' / Struct(
            'foo' / Bytes(1),
            'c' / Struct(
                'foo' / Bytes(1),
                'bar' / Bytes(1)
            )
        )
    )
)

x.parse(b'\xff' * 5)

x.parse(b'\xff' * 3)
# StreamError: Error in path (parsing) -> a -> b -> c -> foo
# stream read less than specified amount, expected 1, found 0

@Evidlo

Evidlo commented Jan 19, 2020 •

Copy link
Copy Markdown
Author

Also, I'd like to propose turning path into a list instead of a string.

Handling the error would like this:

try:
    ...
except ChecksumError as e:
    if e.path == ['parsing', 'foo', 'bar']:
        # do something

which a little bit cleaner than parsing (parsing) -> foo -> bar.

@arekbulski

Copy link
Copy Markdown
Member

I am amazed how much work you put into this. I am going to merge this ASAP. Lets leave the path as a string, it was meant to be human readable (not parsable) and also to show in CI tests (not in exceptions). So, big thanks on behalf of everyone.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants