Repository navigation
Conversation
…or, OneOf, NoneOf
|
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. |
|
Can I split the exceptions out into a separate |
|
No. Look at how different classes raise exceptions that are derived from central ContructError. You can just extend the exception tree. |
|
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 |
|
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. |
|
There's really only 3 solutions with the current codebase:
The first two are the same mentioned previously. I don't think the third way is great because Solution 1 was what I proposed in #731 a few weeks ago |
|
I think I would prefer the third solution, if any. |
|
In solution 3, 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 |
|
I've been considering extending 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 |
|
Is this an acceptable solution? |
|
I like this 3rd path approach. Lets contemplate on this. Are you up to writing a PR? |
I am working on it ATM. EDIT: I finished it. |
Checksum, Check, Const, Validator, ExprValidator, OneOf, NoneOf