fix: improve exception formatting (#146) (#363)

* fix: fall back to exception class name when string representation is empty (#146)

* fix: walk stacktrace and causal chain to extract exception details in format_exception

* fix: fall back to complete stacktrace when exception has no message, as suggested by maintainer

* fix: address maintainer review, push newline control to printing boundaries
This commit is contained in:
UmranPros
2026-06-09 08:27:25 +05:30
committed by GitHub
parent 1a9d01c002
commit ed14dd14ca
3 changed files with 31 additions and 4 deletions
+11 -2
View File
@@ -69,6 +69,7 @@ from .reproduce import collect_reproducibles
from .system import empty_cache, get_accelerator_info from .system import empty_cache, get_accelerator_info
from .utils import ( from .utils import (
format_duration, format_duration,
format_exception,
get_readme_intro, get_readme_intro,
get_trial_parameters, get_trial_parameters,
is_hf_path, is_hf_path,
@@ -364,7 +365,11 @@ def run():
# We cannot recover from this. # We cannot recover from this.
raise raise
print(f"[red]Failed[/] ({error})") formatted = format_exception(error)
if "\n" in formatted:
print(f"[red]Failed[/]:\n{formatted}")
else:
print(f"[red]Failed[/] ({formatted})")
break break
response_lengths = [ response_lengths = [
@@ -1120,7 +1125,11 @@ def run():
print(table) print(table)
except Exception as error: except Exception as error:
print(f"[red]Error: {error}[/]") formatted = format_exception(error)
if "\n" in formatted:
print(f"[red]Error:[/]\n{formatted}")
else:
print(f"[red]Error: {formatted}[/]")
def main(): def main():
+6 -2
View File
@@ -33,7 +33,7 @@ from transformers.generation import (
from .config import QuantizationMethod, RowNormalization, Settings from .config import QuantizationMethod, RowNormalization, Settings
from .system import empty_cache from .system import empty_cache
from .utils import Prompt, batchify, print from .utils import Prompt, batchify, format_exception, print
def get_model_class( def get_model_class(
@@ -150,7 +150,11 @@ class Model:
except Exception as error: except Exception as error:
self.model = None # ty:ignore[invalid-assignment] self.model = None # ty:ignore[invalid-assignment]
empty_cache() empty_cache()
print(f"* [red]Failed[/] ({error})") formatted = format_exception(error)
if "\n" in formatted:
print(f"* [red]Failed[/]:\n{formatted}")
else:
print(f"* [red]Failed[/] ({formatted})")
continue continue
if settings.quantization == QuantizationMethod.BNB_4BIT: if settings.quantization == QuantizationMethod.BNB_4BIT:
+14
View File
@@ -7,6 +7,7 @@ import os
import platform import platform
import random import random
import tempfile import tempfile
import traceback
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timezone from datetime import datetime, timezone
from importlib.metadata import version from importlib.metadata import version
@@ -746,3 +747,16 @@ def upload_reproduce_folder(
repo_id=repo_id, repo_id=repo_id,
token=token, token=token,
) )
def format_exception(error: Exception) -> str:
# Walk causal chain to find a non-empty message.
current = error
while current is not None:
message = str(current).strip()
if message:
return message
current = current.__cause__ or current.__context__
# If there is no message in the entire causal chain, fall back to the complete traceback.
return traceback.format_exc().strip()