Skip to content

fix(observability): pass err via { error } in initErrorRoutes for Winston dedup#3657

Merged
PierreBrisorgueil merged 1 commit into
masterfrom
fix/winston-dedup-string-err
May 11, 2026
Merged

fix(observability): pass err via { error } in initErrorRoutes for Winston dedup#3657
PierreBrisorgueil merged 1 commit into
masterfrom
fix/winston-dedup-string-err

Conversation

@PierreBrisorgueil
Copy link
Copy Markdown
Contributor

@PierreBrisorgueil PierreBrisorgueil commented May 11, 2026

Summary

Files touched

  • lib/services/express.js — 1-line fix in initErrorRoutes
  • lib/services/tests/logger.posthog.transport.unit.tests.js — +1 test (8 total, was 7)

Test plan

  • npm run lint — clean
  • npm run test:unit — 1380 passed (1 new test added)
  • npm run test:coverage — thresholds met (pre-existing $sortArray perf test failure is unrelated to this change, fails on master too)

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced error logging with structured format for improved diagnostics and issue tracking.
  • Tests

    • Added test coverage to verify error transport handling and prevent duplicate error captures.

Review Change Stack

…ston dedup

logger.error(err.stack) passed a string → PostHogErrorTransport couldn't
read posthogCaptured → 2 $exception events per uncaught express error.
Fixes #3656. Adds unit test for info.error dedup path.
Copilot AI review requested due to automatic review settings May 11, 2026 12:01
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0c019508-c17c-4f07-8823-42e710326b73

📥 Commits

Reviewing files that changed from the base of the PR and between a91b07b and 4db4827.

📒 Files selected for processing (2)
  • lib/services/express.js
  • lib/services/tests/logger.posthog.transport.unit.tests.js

Walkthrough

Express error middleware now logs errors as structured objects instead of string stacks, enabling the PostHog transport to read and honor deduplication flags, preventing duplicate exception events.

Changes

Error Deduplication in Express Error Middleware

Layer / File(s) Summary
Express Error Handler Logging
lib/services/express.js
initErrorRoutes middleware changes from logger.error(err.stack) to logger.error('Unhandled express error', { error: err }), passing the error object instead of its string stack to preserve metadata.
PostHog Deduplication Validation
lib/services/tests/logger.posthog.transport.unit.tests.js
New test asserts PostHogErrorTransport skips captureException when info.error.posthogCaptured is already true, while still invoking the log callback.

Possibly related issues

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing the error logging format in initErrorRoutes to enable Winston deduplication.
Description check ✅ Passed The description is comprehensive, covering the issue summary, the one-line fix, test coverage, and validation results. All major template sections are adequately addressed.
Linked Issues check ✅ Passed The code changes fully address #3656: the error is now passed via { error: err } in initErrorRoutes, allowing PostHogErrorTransport to read the posthogCaptured flag for deduplication, and a test verifies the dedup path.
Out of Scope Changes check ✅ Passed All changes are in scope: the one-line fix in express.js and the new unit test in logger.posthog.transport.unit.tests.js directly address the deduplication issue identified in #3656.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/winston-dedup-string-err

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.20%. Comparing base (a91b07b) to head (4db4827).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3657   +/-   ##
=======================================
  Coverage   89.20%   89.20%           
=======================================
  Files         136      136           
  Lines        4668     4668           
  Branches     1451     1451           
=======================================
  Hits         4164     4164           
  Misses        392      392           
  Partials      112      112           
Flag Coverage Δ
integration 59.59% <100.00%> (ø)
unit 64.35% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a91b07b...4db4827. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Fixes PostHog Error Tracking deduplication for uncaught Express errors by ensuring Winston logs carry the original Error object (so the posthogCaptured flag can be read by the PostHog transport), and adds a unit test covering the { error: err } metadata dedup path.

Changes:

  • Update Express error route logging to call logger.error(..., { error: err }) instead of logging err.stack as a plain string.
  • Add a unit test to ensure PostHogErrorTransport skips capture when info.error.posthogCaptured === true.

Reviewed changes

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

File Description
lib/services/express.js Adjusts uncaught Express error logging to pass the Error object via Winston metadata for PostHog dedup.
lib/services/tests/logger.posthog.transport.unit.tests.js Adds coverage for the dedup path when the Error is provided in info.error.

Comment thread lib/services/express.js
Comment on lines 266 to 269
app.use((err, req, res, next) => {
if (!err) return next();
logger.error(err.stack);
logger.error('Unhandled express error', { error: err });
res.status(err.status || 500).send({
@PierreBrisorgueil PierreBrisorgueil merged commit 829b91b into master May 11, 2026
11 checks passed
@PierreBrisorgueil PierreBrisorgueil deleted the fix/winston-dedup-string-err branch May 11, 2026 12:06
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.

fix(observability): logger.error(err.stack) bypasses Winston PostHog dedup → 2 $exception events per uncaught error

2 participants