Commit 2bd665ad593f2f43e4c52521c4df24f2361b44f1
1 parent
90f25d4c
msodde: avoid misleading stack traces from fail in except block
Showing
1 changed file
with
14 additions
and
11 deletions
oletools/msodde.py
| ... | ... | @@ -818,20 +818,23 @@ def process_file(filepath, field_filter_mode=None): |
| 818 | 818 | return process_xls(filepath) |
| 819 | 819 | else: |
| 820 | 820 | return process_doc(filepath) |
| 821 | - else: | |
| 822 | - with open(filepath, 'rb') as file_handle: | |
| 823 | - if file_handle.read(4) == RTF_START: | |
| 824 | - # This is a RTF file | |
| 825 | - return process_rtf(file_handle, field_filter_mode) | |
| 821 | + | |
| 822 | + with open(filepath, 'rb') as file_handle: | |
| 823 | + if file_handle.read(4) == RTF_START: | |
| 824 | + # This is a RTF file | |
| 825 | + return process_rtf(file_handle, field_filter_mode) | |
| 826 | + | |
| 826 | 827 | try: |
| 827 | 828 | doctype = ooxml.get_type(filepath) |
| 828 | - log.debug('Detected file type: {0}'.format(doctype)) | |
| 829 | - if doctype == ooxml.DOCTYPE_EXCEL: | |
| 830 | - return process_xlsx(filepath, field_filter_mode) | |
| 831 | - else: | |
| 832 | - return process_docx(filepath, field_filter_mode) | |
| 833 | 829 | except Exception: |
| 834 | 830 | log.debug('Exception trying to xml-parse file', exc_info=True) |
| 831 | + doctype = None | |
| 832 | + | |
| 833 | + if doctype: | |
| 834 | + log.debug('Detected file type: {0}'.format(doctype)) | |
| 835 | + if doctype == ooxml.DOCTYPE_EXCEL: | |
| 836 | + return process_xlsx(filepath, field_filter_mode) | |
| 837 | + else: | |
| 835 | 838 | return process_docx(filepath, field_filter_mode) |
| 836 | 839 | |
| 837 | 840 | |
| ... | ... | @@ -881,7 +884,7 @@ def main(cmd_line_args=None): |
| 881 | 884 | jout.append(dict(type='error', error=type(exc).__name__, |
| 882 | 885 | message=str(exc))) # strange: str(exc) is enclosed in "" |
| 883 | 886 | else: |
| 884 | - raise | |
| 887 | + raise # re-raise last known exception, keeping trace intact | |
| 885 | 888 | |
| 886 | 889 | if args.json: |
| 887 | 890 | for line in text.splitlines(): | ... | ... |