Skip to content

fix: improve exception formatting (#146)#363

Merged
p-e-w merged 4 commits into
p-e-w:masterfrom
umran666:fix/empty-exception-messages
Jun 9, 2026
Merged

fix: improve exception formatting (#146)#363
p-e-w merged 4 commits into
p-e-w:masterfrom
umran666:fix/empty-exception-messages

Conversation

@umran666

@umran666 umran666 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Falls back to printing the exception's class name in main.py and model.py if the exception's string representation is empty, preventing empty parentheses like Failed () from being printed for exceptions without messages.

Closes #146.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves error reporting in src/heretic/main.py and src/heretic/model.py by stripping whitespace from caught exception messages and falling back to the exception's class name if the string representation is empty. No review comments were provided, so there is no feedback to address.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@p-e-w

p-e-w commented Jun 8, 2026

Copy link
Copy Markdown
Owner

This approach has been proposed before in #183, but it doesn't really improve things. Failed (RuntimeError) isn't any better than Failed ().

A real solution might involve walking the stacktrace to find a useful error message, but in any case I'd need to see a concrete error case to see whether it's actually an improvement or not.

@umran666

umran666 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

A real solution might involve walking the stacktrace to find a useful error message, but in any case I'd need to see a concrete error case to see whether it's actually an improvement or not.

Good point. I've updated the PR to address this and make the error reporting a lot more useful

  • If the exception is a wrapper with no message (but wraps an underlying exception that does have one), it walks the causal chain (cause/context) to pull out the actual inner error message.
  • If there is absolutely no message anywhere, it falls back to showing the exception class name along with the file and line number where the error was actually raised (e.g. RuntimeError at model.py:153).
    This way we don't just print a generic error type or empty parentheses, and instead tell the user exactly where the load failed without dumping a full traceback.

@p-e-w

p-e-w commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Can you show the output of an actual error with this mechanism?

@umran666

umran666 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Can you show the output of an actual error with this mechanism?

Yea I can show them Here are a few concrete examples of how the output looks under this mechanism compared to the previous code:

  1. Bare RuntimeError (with no message)
    Before: Failed ()
    After: Failed (RuntimeError at model.py:153)

  2. Nested / Chained Exception (e.g. RuntimeError wrapping a ValueError with an inner config message)
    Before: Failed ()
    After: Failed (invalid model configuration: layer_norm_eps must be positive)

  3. Normal Exception (with a message, e.g., CUDA OOM)
    Before: Failed (CUDA out of memory...)
    After: Failed (CUDA out of memory...) (no class name prefix noise)

@p-e-w

p-e-w commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Those are inferred examples. I'm asking for the actual output of an error that really happened.

Also, assuming that "RuntimeError at model.py:153" is what the mechanism might actually produce, it makes no sense. Of course the stack passed through the place where it is printed, but that doesn't tell us anything. We want to know the cause of the error.

@p-e-w

p-e-w commented Jun 8, 2026

Copy link
Copy Markdown
Owner

I'm quite honestly wondering if we are overthinking this. Perhaps, if there is no message, we should simply print the complete stacktrace.

@umran666

umran666 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Also, assuming that "RuntimeError at model.py:153" is what the mechanism might actually produce, it makes no sense. Of course the stack passed through the place where it is printed, but that doesn't tell us anything. We want to know the cause of the error.

Good point, printing the complete stacktrace makes a lot more sense if there's no message. I've updated the PR to do exactly that:

If there's an underlying exception in the causal chain that does have a message, it still extracts that message cleanly.
If the entire chain is completely empty (no message anywhere), it falls back to printing the complete, standard traceback on a new line.
Here is the actual output under the updated code:

  1. Bare RuntimeError (with no message)
  • Trying dtype bfloat16... Failed:
    Traceback (most recent call last):
    File "C:\Users\shaik\heretic\src\heretic\model.py", line 141, in init
    self.generate(
    File "C:\Users\shaik\heretic\src\heretic\model.py", line 628, in generate
    outputs = self.model.generate(
    ^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\shaik\heretic.venv\Lib\site-packages\transformers\generation\utils.py", line 2543, in generate
    result = decoding_method(
    ^^^^^^^^^^^^^^^^
    ...
    RuntimeError
  1. Nested Exception (extracts the real underlying error message)
  • Trying dtype bfloat16... Failed (invalid model configuration: layer_norm_eps must be positive)

Comment thread src/heretic/utils.py Outdated
current = current.__cause__ or current.__context__

# If there is no message in the entire causal chain, fall back to the complete traceback.
return "\n" + traceback.format_exc().strip()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the formatted exception shouldn't start with a newline as a signal. That's a semantic mismatch. The newline should be added when printing.

@umran666

umran666 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR to return clean traceback strings from format_exception without the leading newline marker, moving the layout control entirely to the print calls.

@p-e-w p-e-w changed the title fix: fall back to exception class name when string is empty (#146) fix: improve exception formatting (#146) Jun 9, 2026
@p-e-w p-e-w merged commit ed14dd1 into p-e-w:master Jun 9, 2026
5 checks passed
@p-e-w

p-e-w commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Yup, that does the trick. Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Printing exceptions outputs empty message if the exception has no message attached, complicating debugging

2 participants