Skip to content

Improve error handling #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/pysweepme/DeviceManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def get_driver_module(folder: str, name: str) -> types.ModuleType:
except Exception as e: # noqa: BLE001
# We don't know what could go wrong, so we catch all exceptions, log the error, and raise an Exception again
error()
msg = f"Cannot load Driver '{name}' from folder {folder}. Please change folder or copy Driver to your project."
msg = f"Cannot load Driver '{name}' from folder {folder}."
if isinstance(e, FileNotFoundError):
msg += " Please change folder or copy Driver to your project."
raise ImportError(msg) from e

return module
Expand Down
79 changes: 53 additions & 26 deletions src/pysweepme/ErrorMessage.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# The MIT License

#
# Copyright (c) 2021-2022 SweepMe! GmbH

#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:

#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -20,11 +20,44 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import annotations

from traceback import print_exc
import os
from time import localtime
from traceback import print_exc
from typing import Callable

import os

def try_to_print_traceback() -> None:
"""Print a traceback for the most recent python exception.

If there is a UnicodeDecodeError while trying to print the exception, e.g.
because a Python source file containing an error is using non UTF-8 encoding,
the function tries to monkey patch the traceback module to skip reading lines
from the file and still try to print as many error details as possible.
"""
try:
print_exc()
except UnicodeDecodeError:
import traceback

# monkeypatching the linecache module
# so UnicodeDecodeErrors while trying to read a file don't lead to an
# uncaught Exception but only "empty" lines in the traceback
original_updatecache: Callable[
[str, dict[str, object] | None],
list[str],
] = traceback.linecache.updatecache # type: ignore[attr-defined]

def try_updatecache(filename: str, module_globals: dict[str, object] | None = None) -> list[str]:
try:
return original_updatecache(filename, module_globals)
except Exception: # noqa: BLE001 - error handling should also catch any unexpected errors
print("<Failed to read file contents>") # noqa: T201
return []

traceback.linecache.updatecache = try_updatecache # type: ignore[attr-defined]
print_exc()


def error(*args: object) -> None:
Expand All @@ -34,34 +67,28 @@ def error(*args: object) -> None:
*args: The arguments to print to the debug log.
"""
year, month, day, hour, min, sec = localtime()[:6]
print("-"*60)
print('Time: %s.%s.%s %02d:%02d:%02d' % (day, month, year, hour, min, sec))
print("-" * 60)
print("Time: %s.%s.%s %02d:%02d:%02d" % (day, month, year, hour, min, sec))
if len(args) > 0:
print('Message:', *args)
print('Python Error:')
print_exc()
print('-'*60)
print("Message:", *args)
print("Python Error:")
try_to_print_traceback()
print("-" * 60)


def debug(*args: object, debugmode_only: bool = False) -> None:
"""Print arguments to the debug log.

Args:
*args: The arguments to print to the debug log.
debugmode_only: True if the arguments shall be printed only when debug mode is on.
"""
if "SWEEPME_DEBUGMODE" in os.environ:
debug_mode = os.environ["SWEEPME_DEBUGMODE"] == "True"
else:
debug_mode = False

if not debugmode_only or debug_mode:

if len(args) > 0:

year, month, day, hour, min, sec = localtime()[:6]
print("-"*60)
print('Debug: %s.%s.%s %02d:%02d:%02d\t' % (day, month, year, hour, min, sec), *args)
debug_mode = os.environ["SWEEPME_DEBUGMODE"] == "True" if "SWEEPME_DEBUGMODE" in os.environ else False

if (not debugmode_only or debug_mode) and len(args) > 0:
year, month, day, hour, min, sec = localtime()[:6]
print("-" * 60)
print("Debug: %s.%s.%s %02d:%02d:%02d\t" % (day, month, year, hour, min, sec), *args)


def debug_only(*args: object) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/pysweepme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# SOFTWARE.


__version__ = "1.5.6.12"
__version__ = "1.5.6.13"

import sys

Expand Down
Loading