From fec96b92a7305130133a0a383b378145b5cbac05 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Thu, 1 Aug 2024 00:20:53 -0700 Subject: [PATCH] improve error handling --- mem_edit/abstract.py | 2 +- mem_edit/windows.py | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mem_edit/abstract.py b/mem_edit/abstract.py index 5305116..8e1e2f3 100644 --- a/mem_edit/abstract.py +++ b/mem_edit/abstract.py @@ -341,7 +341,7 @@ class Process(metaclass=ABCMeta): self.read_memory(start, region_buffer) found += [offset + start for offset in search(needle_buffer, region_buffer)] except OSError: - logger.error('Failed to read in range 0x{} - 0x{}'.format(start, stop)) + logger.exception(f'Failed to read in range 0x{start} - 0x{stop}') return found @classmethod diff --git a/mem_edit/windows.py b/mem_edit/windows.py index 49f92eb..9de6b06 100644 --- a/mem_edit/windows.py +++ b/mem_edit/windows.py @@ -161,8 +161,8 @@ class Process(AbstractProcess): ctypes.sizeof(write_buffer), None ) - except (BufferError, ValueError, TypeError): - raise MemEditError(f'Error with handle {self.process_handle}: {self._get_last_error()}') + except (BufferError, ValueError, TypeError) as err: + raise MemEditError(f'Error with handle {self.process_handle}: {self._get_last_error()}') from err def read_memory(self, base_address: int, read_buffer: ctypes_buffer_t) -> ctypes_buffer_t: try: @@ -173,8 +173,8 @@ class Process(AbstractProcess): ctypes.sizeof(read_buffer), None ) - except (BufferError, ValueError, TypeError): - raise MemEditError(f'Error with handle {self.process_handle}: {self._get_last_error()}') + except (BufferError, ValueError, TypeError) as err: + raise MemEditError(f'Error with handle {self.process_handle}: {self._get_last_error()}') from err return read_buffer @@ -274,8 +274,7 @@ class Process(AbstractProcess): if success == 0: raise MemEditError('Failed VirtualQueryEx with handle ' + f'{self.process_handle}: {self._get_last_error()}') - else: - raise MemEditError('VirtualQueryEx output too short!') + raise MemEditError('VirtualQueryEx output too short!') return mbi