Skip to content

Commit

Permalink
Add :report behavior to ActiveSupport::Deprecation
Browse files Browse the repository at this point in the history
This behavior uses the ErrorReporter to report a deprecation as a
handled error with :warning severity.
  • Loading branch information
etiennebarrie committed Jun 26, 2023
1 parent 4f36572 commit 78c7343
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
* Add `:report` behavior for Deprecation

Setting `config.active_support.deprecation = :report` uses the error
reporter to report deprecation warnings to `ActiveSupport::ErrorReporter`.

Deprecations are reported as handled errors, with a severity of `:warning`.

Useful to report deprecations happening in production to your bug tracker.

*Étienne Barrié*

* Rename `Range#overlaps?` to `#overlap?` and add alias for backwards compatibility

*Christian Schmidt*
Expand Down
8 changes: 8 additions & 0 deletions activesupport/lib/active_support/deprecation/behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Deprecation
end,

silence: ->(message, callstack, deprecator) { },

report: ->(message, callstack, deprecator) do
error = DeprecationException.new(message)
error.set_backtrace(callstack.map(&:to_s))
ActiveSupport.error_reporter.report(error)
end
}

# Behavior module allows to determine how to display deprecation messages.
Expand All @@ -55,6 +61,7 @@ class Deprecation
# [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
# [+log+] Log all deprecation warnings to +Rails.logger+.
# [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
# [+report+] Use +ActiveSupport::ErrorReporter+ to report deprecations.
# [+silence+] Do nothing. On \Rails, set <tt>config.active_support.report_deprecations = false</tt> to disable all behaviors.
#
# Setting behaviors only affects deprecations that happen after boot time.
Expand Down Expand Up @@ -82,6 +89,7 @@ def disallowed_behavior
# [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
# [+log+] Log all deprecation warnings to +Rails.logger+.
# [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
# [+report+] Use +ActiveSupport::ErrorReporter+ to report deprecations.
# [+silence+] Do nothing.
#
# Setting behaviors only affects deprecations that happen after boot time.
Expand Down
11 changes: 11 additions & 0 deletions activesupport/test/deprecation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ def setup
end
end

test ":report_error behavior" do
@deprecator = ActiveSupport::Deprecation.new("horizon", "MyGem::Custom")
@deprecator.behavior = :report
report = assert_error_reported(ActiveSupport::DeprecationException) do
@deprecator.warn
end
assert_equal true, report.handled
assert_equal :warning, report.severity
assert_equal "application", report.source
end

test "invalid behavior" do
e = assert_raises(ArgumentError) do
@deprecator.behavior = :invalid
Expand Down

0 comments on commit 78c7343

Please sign in to comment.