Commit 140452821f3a8c33742ba6a63fe8f1482c2b843a

Authored by Christian Herdtweck
1 parent 3dedfa70

create exception class FileOpenError, raise from VBA_Parser if all open_... failed

Showing 1 changed file with 17 additions and 1 deletions
oletools/olevba.py
... ... @@ -290,6 +290,20 @@ def get_logger(name, level=logging.CRITICAL+1):
290 290 log = get_logger('olevba')
291 291  
292 292  
  293 +#=== EXCEPTIONS ==============================================================
  294 +
  295 +class FileOpenError(Exception):
  296 + """ raised by VBA_Parser constructor if all open_... attempts failed
  297 +
  298 + probably means the file type is not supported
  299 + """
  300 +
  301 + def __init__(self, filename):
  302 + super(InvalidFileTypeError, self).__init__(
  303 + 'Failed to open file %s ... probably not supported' % filename)
  304 + self.filename = filename
  305 +
  306 +
293 307 #--- CONSTANTS ----------------------------------------------------------------
294 308  
295 309 # URL and message to report issues:
... ... @@ -1908,6 +1922,8 @@ class VBA_Parser(object):
1908 1922  
1909 1923 :param container: str, path and filename of container if the file is within
1910 1924 a zip archive, None otherwise.
  1925 +
  1926 + raises a FileOpenError if all attemps to interpret the data header failed
1911 1927 """
1912 1928 #TODO: filename should only be a string, data should be used for the file-like object
1913 1929 #TODO: filename should be mandatory, optional data is a string or file-like object
... ... @@ -1985,7 +2001,7 @@ class VBA_Parser(object):
1985 2001 # At this stage, could not match a known format:
1986 2002 msg = '%s is not a supported file type, cannot extract VBA Macros.' % self.filename
1987 2003 log.error(msg)
1988   - raise TypeError(msg)
  2004 + raise FileOpenError(msg)
1989 2005  
1990 2006 def open_ole(self, _file):
1991 2007 """
... ...