Commit a7093b9b4bca4691fd2ff6eaef9c1bc02ffcb5ea

Authored by Christian Herdtweck
1 parent f2d57983

created 3 new Exceptions: SubfileOpenError, UnexpectedDataError and OlevbaBaseException

made all Exceptions subclasses of OlevbaBaseException,
move some common functionality there
Showing 1 changed file with 46 additions and 12 deletions
oletools/olevba.py
@@ -299,35 +299,69 @@ log = get_logger('olevba') @@ -299,35 +299,69 @@ log = get_logger('olevba')
299 299
300 #=== EXCEPTIONS ============================================================== 300 #=== EXCEPTIONS ==============================================================
301 301
302 -class FileOpenError(Exception): 302 +class OlevbaBaseException(Exception):
  303 + """ Base class for exceptions produced here for simpler except clauses """
  304 + def __init__(self, msg, filename=None, orig_exc=None, **kwargs):
  305 + if orig_exc:
  306 + super(OlevbaBaseException, self).__init__(msg +
  307 + ' ({})'.format(orig_exc),
  308 + **kwargs)
  309 + else:
  310 + super(OlevbaBaseException, self).__init__(msg, **kwargs)
  311 + self.msg = msg
  312 + self.filename = filename
  313 + self.orig_exc = orig_exc
  314 +
  315 +
  316 +class FileOpenError(OlevbaBaseException):
303 """ raised by VBA_Parser constructor if all open_... attempts failed 317 """ raised by VBA_Parser constructor if all open_... attempts failed
304 318
305 probably means the file type is not supported 319 probably means the file type is not supported
306 """ 320 """
307 321
308 - def __init__(self, filename): 322 + def __init__(self, filename, orig_exc=None):
309 super(FileOpenError, self).__init__( 323 super(FileOpenError, self).__init__(
310 - 'Failed to open file %s ... probably not supported' % filename)  
311 - self.filename = filename 324 + 'Failed to open file %s' % filename, filename, orig_exc)
312 325
313 326
314 -class ProcessingError(Exception): 327 +class ProcessingError(OlevbaBaseException):
315 """ raised by VBA_Parser.process_file* functions """ 328 """ raised by VBA_Parser.process_file* functions """
316 329
317 - def __init__(self, filename, orig_exception): 330 + def __init__(self, filename, orig_exc):
318 super(ProcessingError, self).__init__( 331 super(ProcessingError, self).__init__(
319 - 'Error processing file %s (%s)' % (filename, orig_exception))  
320 - self.filename = filename  
321 - self.orig_exception = orig_exception 332 + 'Error processing file %s' % filename, filename, orig_exc)
322 333
323 334
324 -class MsoExtractionError(RuntimeError): 335 +class MsoExtractionError(RuntimeError, OlevbaBaseException):
325 """ raised by mso_file_extract if parsing MSO/ActiveMIME data failed """ 336 """ raised by mso_file_extract if parsing MSO/ActiveMIME data failed """
326 337
327 def __init__(self, msg): 338 def __init__(self, msg):
328 - super(MsoExtractionError, self).__init__(msg)  
329 - self.msg = msg 339 + MsoExtractionError.__init__(self, msg)
  340 + OlevbaBaseException.__init__(self, msg)
  341 +
  342 +
  343 +class SubstreamOpenError(FileOpenError):
  344 + """ special kind of FileOpenError: file is a substream of original file """
  345 +
  346 + def __init__(self, filename, subfilename, orig_exc=None):
  347 + super(SubstreamOpenError, self).__init__(
  348 + str(filename) + '/' + str(subfilename), orig_exc)
  349 + self.filename = filename # overwrite setting in OlevbaBaseException
  350 + self.subfilename = subfilename
  351 +
  352 +
  353 +class UnexpectedDataError(OlevbaBaseException):
  354 + """ raised when parsing is strict (=not relaxed) and data is unexpected """
330 355
  356 + def __init__(self, stream_path, variable, expected, value):
  357 + super(UnexpectedDataError, self).__init__(self,
  358 + 'Unexpected value in {} for variable {}: '
  359 + 'expected {:04X but found {:04X}!'
  360 + .format(stream_path, variable, expected, value))
  361 + self.stream_path = stream_path
  362 + self.variable = variable
  363 + self.expected = expected
  364 + self.value = value
331 365
332 #--- CONSTANTS ---------------------------------------------------------------- 366 #--- CONSTANTS ----------------------------------------------------------------
333 367