Skip to content

fix: handle circular error references in logger and handlers (GIT-109)#75

Merged
TonyCasey merged 1 commit intomainfrom
GIT-109
Feb 15, 2026
Merged

fix: handle circular error references in logger and handlers (GIT-109)#75
TonyCasey merged 1 commit intomainfrom
GIT-109

Conversation

@TonyCasey
Copy link
Copy Markdown
Owner

@TonyCasey TonyCasey commented Feb 15, 2026

Summary

  • Logger's JSON.stringify crashed on Error objects with circular references, causing CommitMsgHandler to fail silently and skip AI trailers on commits
  • Added safeStringify to Logger with circular reference detection and Error object serialization (message + stack)
  • Updated all five hook handlers (CommitMsgHandler, PostCommitHandler, PromptSubmitHandler, SessionStartHandler, SessionStopHandler) to extract error.message and error.stack before passing to logger context

Test plan

  • 476 unit tests pass
  • 91 integration tests pass
  • Build succeeds with no type errors
  • Verified trailers are added correctly on local test commit

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved error handling and logging consistency across the application to provide more detailed error information
    • Enhanced error serialization to properly handle edge cases in logs

Logger JSON.stringify crashed on Error objects with circular references,
causing the CommitMsgHandler to fail silently and skip trailers. Added
safeStringify with circular reference detection and Error serialization
to the Logger. Also updated all five hook handlers to extract error
message/stack before passing to logger context instead of raw Error
objects.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
AI-Agent: Claude-Code/2.1.42
Copilot AI review requested due to automatic review settings February 15, 2026 21:47
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 15, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

This PR standardizes error handling across multiple application handlers by normalizing caught errors into proper Error instances, enhancing logging to capture both message and stack trace, and ensuring returned error objects use the normalized Error. It also adds a safeStringify helper to handle circular references and Error serialization in logging.

Changes

Cohort / File(s) Summary
Application Handlers Error Normalization
src/application/handlers/CommitMsgHandler.ts, src/application/handlers/PostCommitHandler.ts, src/application/handlers/PromptSubmitHandler.ts, src/application/handlers/SessionStartHandler.ts, src/application/handlers/SessionStopHandler.ts
Standardized error handling in catch blocks: errors are normalized into Error instances (wrapping non-Error values), logging now captures both message and stack from the normalized error, and returned error objects use the normalized Error instance instead of the raw caught value.
Logging Infrastructure
src/infrastructure/logging/Logger.ts
Added safeStringify helper function to safely JSON.stringify objects with circular reference handling and Error serialization. Logger now uses safeStringify for context string construction instead of direct JSON.stringify, improving serialization robustness for Error objects and circular structures.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing circular error reference handling in the logger and handlers, which directly aligns with the changeset's core objective of preventing crashes and enabling proper error serialization.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch GIT-109

Comment @coderabbitai help to get the list of available commands and usage tips.

@TonyCasey TonyCasey merged commit 337a4d1 into main Feb 15, 2026
9 checks passed
@TonyCasey TonyCasey deleted the GIT-109 branch February 15, 2026 21:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug where circular references in Error objects caused JSON.stringify to crash in the Logger, leading to silent failures in CommitMsgHandler and other handlers that prevented AI trailers from being added to commits.

Changes:

  • Added safeStringify utility to Logger that detects circular references using WeakSet and serializes Error objects to plain objects with only message and stack properties
  • Updated all five hook handlers to extract error.message and error.stack before passing to logger context, providing defense-in-depth against circular reference issues

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/infrastructure/logging/Logger.ts Added safeStringify function with circular reference detection and Error object serialization; replaced JSON.stringify with safeStringify in logger context formatting
src/application/handlers/CommitMsgHandler.ts Updated catch block to extract error message and stack before logging
src/application/handlers/PostCommitHandler.ts Updated catch block to extract error message and stack before logging
src/application/handlers/PromptSubmitHandler.ts Updated catch block to extract error message and stack before logging
src/application/handlers/SessionStartHandler.ts Updated catch block to extract error message and stack before logging
src/application/handlers/SessionStopHandler.ts Updated catch block to extract error message and stack before logging

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25 to +38
/** JSON.stringify that handles circular references and Error objects. */
function safeStringify(obj: unknown): string {
const seen = new WeakSet();
return JSON.stringify(obj, (_key, value) => {
if (value instanceof Error) {
return { message: value.message, stack: value.stack };
}
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]';
seen.add(value);
}
return value;
});
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

The new safeStringify function lacks test coverage. This is critical functionality that prevents silent logger failures, and should have tests covering:

  • Circular reference detection (e.g., const obj = {}; obj.self = obj;)
  • Error object serialization (e.g., new Error('test'))
  • Error objects with circular references in their properties
  • Nested objects with mixed circular references and Error objects
  • Null and undefined values
  • Primitives (strings, numbers, booleans)

Consider adding a test file at tests/unit/infrastructure/logging/safeStringify.test.ts or adding these tests to the existing Logger.test.ts file.

Copilot uses AI. Check for mistakes.
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.

2 participants