IGNORE: Standardize exception logging format and add enforcement cop#5583
Draft
IGNORE: Standardize exception logging format and add enforcement cop#5583
Conversation
Replace `e.message` with `e` (via string interpolation) and
`e.class.name` with `e.class` in all logging contexts across
lib/datadog/.
The `to_s` and `message` methods on Exception have different contracts:
subclasses can override them independently, and `to_s` is the method
Ruby calls during string interpolation. Using `e.message` directly
can produce different output than `#{e}` when a subclass overrides
`to_s` without overriding `message`, or vice versa.
Similarly, `e.class` already returns a displayable class name via
`to_s`, making `.name` redundant in string interpolation contexts.
The codebase convention is `#{e.class}: #{e}`. This commit brings
all logging call sites in line with that convention.
Does not change:
- `e.message` used for constructing new exceptions (raise)
- `e.message` used in data structures or API payloads
- `e.message` in Core::Error.build_from (value object construction)
- `e.message` in conditionals or comparisons
- Test files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The source code changed from `e.message` / `e.class.name e.message` to `e.class: e` (which produces `ClassName: message`). Test expectations that asserted on the old format now fail because the logged string includes the exception class name with a colon separator. - tags_spec.rb: expected "Oops..." → "StandardError: Oops..." - component_spec.rb: expected /Test failure/ → /RuntimeError: Test failure/ (3 tests: start, stop, update_on_fork) - transport_spec.rb: expected /Timeout::Error Ooops/ → /Timeout::Error: Ooops/ (space → colon+space between class and message) Co-Authored-By: Claude <noreply@anthropic.com>
debug() with string interpolation always constructs the string even when
debug logging is off. Block form (debug { }) defers construction.
Also standardize e.message in open_feature/remote.rb which was missed
in the initial pass.
Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
|
✅ Tests 🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: e819a2d | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback! |
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>
- 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>
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>
c6a68a8 to
e819a2d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Combines #5514 (standardize exception logging) with #5582 (custom cop to enforce it).
lib/datadog/to"#{e.class}: #{e}"(59 files)CustomCops::ExceptionMessageCopto prevent regressionsThe cop detects three patterns:
e.message→ should bee(to_sandmessagehave different contracts;#{e}callsto_s, which is the convention)e.class.name→ should bee.class(Class#to_salready returns the name)#{e}without#{e.class}in the same string → the convention requires the class nameAuto-corrects
e.messageande.class.namewithin string interpolation. The missing-class check flags but does not auto-correct.Motivation:
Exception#to_sandException#messagehave different contracts in Ruby. Subclasses can override them independently, andto_sis the method Ruby calls during string interpolation. The codebase convention is"#{e.class}: #{e}". This PR brings all call sites in line and adds a cop to keep them there. Cop suggested by @vpellan in #5514 review.Change log entry
None.
Additional Notes:
Supersedes #5514 and #5582 — those can be closed if this one merges.
Cop only applies to
lib/**/*files (same scope as other custom cops).How to test the change?