From f14ac8e2f29a2002c69f6a15ffc2f4af4a58c08b Mon Sep 17 00:00:00 2001 From: Marcel Zwiers Date: Fri, 7 Feb 2025 00:43:28 +0100 Subject: [PATCH] Prepend traceback info to trackusage exception messages --- bidscoin/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bidscoin/__init__.py b/bidscoin/__init__.py index 22b1a0b8..90694fde 100644 --- a/bidscoin/__init__.py +++ b/bidscoin/__init__.py @@ -26,6 +26,7 @@ import warnings import tempfile import subprocess +import traceback from pathlib import Path from importlib import metadata from typing import Union @@ -191,6 +192,9 @@ def trackusage(event: str, message='', dryrun: bool=False) -> dict: 'userid': hashlib.md5(getpass.getuser().encode('utf8')).hexdigest(), 'hostid': hashlib.md5(platform.node().encode('utf8')).hexdigest()} if message: + if isinstance(message, Exception): + trace = traceback.extract_tb(traceback.sys.exc_info()[2])[-1] # Get the last traceback entry + message = f"({trace.filename},{trace.lineno}){message}" # Prepend the traceback info data['message'] = str(message) if container := os.getenv('CONTAINER'): data['container'] = container @@ -211,7 +215,7 @@ def trackusage(event: str, message='', dryrun: bool=False) -> dict: return data tracked[event] = now - # If something goes wrong, add an error message, clear the shelf and return + # If something goes wrong, add an error message, clear the shelf and return if we can't sleep except Exception as shelveerror: data['event'] = 'trackusage_exception' data['message'] = f"({event}){shelveerror}"