Skip to content

Add custom RuboCop cop for consistent exception logging format#5582

Draft
p-datadog wants to merge 4 commits intomasterfrom
custom-cop/exception-log-format
Draft

Add custom RuboCop cop for consistent exception logging format#5582
p-datadog wants to merge 4 commits intomasterfrom
custom-cop/exception-log-format

Conversation

@p-datadog
Copy link
Copy Markdown
Member

@p-datadog p-datadog commented Apr 10, 2026

What does this PR do?
Adds CustomCops::ExceptionMessageCop that enforces consistent exception logging format inside rescue blocks.

The cop detects three patterns:

  • e.message → should be e (to_s and message have different contracts; #{e} calls to_s, which is the convention)
  • e.class.name → should be e.class (Class#to_s already returns the name)
  • #{e} without #{e.class} in the same string → the convention requires the class name

Auto-corrects e.message and e.class.name within string interpolation. The missing-class check flags but does not auto-correct.

Motivation:
Follow-up to #5514 which standardized #{e.class}: #{e} across the codebase. This cop prevents the old patterns from being reintroduced. Suggested by @vpellan during review.

Change log entry
None.

Additional Notes:
Only applies to lib/**/* files (same scope as other custom cops).

How to test the change?

bundle exec rake spec:custom_cop

Detects `e.message` and `e.class.name` inside rescue blocks and
auto-corrects them to `e` and `e.class` within string interpolation.

Follow-up to PR #5514 which standardized the format repo-wide.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@p-datadog p-datadog added the AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos label Apr 10, 2026
p-ddsign and others added 2 commits April 10, 2026 16:04
- Fix offense messages: to_s and message have different contracts,
  not the same value. The convention prefers to_s (via interpolation).
- Use <<~'RUBY' (non-interpolating) heredocs in specs so #{} is
  literal and caret column positions align correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Detect bare #{e} without #{e.class} in the same string inside rescue
blocks. The codebase convention is "#{e.class}: #{e}" — logging the
exception without its class loses context.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@datadog-prod-us1-4
Copy link
Copy Markdown

datadog-prod-us1-4 bot commented Apr 10, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 95.35% (-0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 4f3fde9 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

The e.message caret in the combined-patterns test was 1 column too far
right (col 26 instead of col 25). Verified all caret positions against
actual cop output.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants