Skip to content

Commit 104f311

Browse files
committed
Added system of exception
1 parent 0e3f6e0 commit 104f311

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

kaitaistruct.py

+38
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,41 @@ def resolve_enum(enum_obj, value):
380380
return enum_obj(value)
381381
except ValueError:
382382
return value
383+
384+
385+
class KaitaiStructError(BaseException):
386+
"""Common ancestor for all error originating from Kaitai Struct usage.
387+
Stores KSY source path, pointing to an element supposedly guilty of
388+
an error.
389+
"""
390+
def __init__(self, msg, src_path):
391+
super(KaitaiStructError, self).__init__("%s: %s" % (src_path, msg))
392+
self.src_path = src_path
393+
394+
395+
class UndecidedEndiannessError(KaitaiStructError):
396+
"""Error that occurs when default endianness should be decided with
397+
switch, but nothing matches (although using endianness expression
398+
implies that there should be some positive result).
399+
"""
400+
def __init__(self, src_path):
401+
super(KaitaiStructError, self).__init__("unable to decide on endianness for a type", src_path)
402+
403+
404+
class ValidationFailedError(KaitaiStructError):
405+
"""Common ancestor for all validation failures. Stores pointer to
406+
KaitaiStream IO object which was involved in an error.
407+
"""
408+
def __init__(self, msg, io, src_path):
409+
super(ValidationFailedError, self).__init__("at pos %d: validation failed: %s" % (io.pos(), msg), src_path)
410+
self.io = io
411+
412+
413+
class ValidationNotEqualError(ValidationFailedError):
414+
"""Signals validation failure: we required "actual" value to be equal to
415+
"expected", but it turned out that it's not.
416+
"""
417+
def __init__(self, expected, actual, io, src_path):
418+
super(ValidationNotEqualError, self).__init__("not equal, expected %s, but got %s" % (repr(expected), repr(actual)), io, src_path)
419+
self.expected = expected
420+
self.actual = actual

0 commit comments

Comments
 (0)