Skip to content

Commit

Permalink
Remove listen_for_error helper (#1230)
Browse files Browse the repository at this point in the history
This was deprecated in PR #1229, we can remove it in the next major
version.
  • Loading branch information
tombruijn authored Aug 5, 2024
1 parent 41035ba commit 7c23292
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 138 deletions.
6 changes: 6 additions & 0 deletions .changesets/remove-listen_for_error-helper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: major
type: remove
---

Remove the `Appsignal.listen_for_error` helper. Use manual exception handling using `rescue => error` with the `Appsignal.report_error` helper instead.
49 changes: 0 additions & 49 deletions lib/appsignal/helpers/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,55 +158,6 @@ def monitor_and_stop(action:, namespace: nil, &block)
Appsignal.stop("monitor_and_stop")
end

# Listen for an error to occur and send it to AppSignal.
#
# Uses {.send_error} to directly send the error in a separate
# transaction. Does not add the error to the current transaction.
#
# Make sure that AppSignal is integrated in your application beforehand.
# AppSignal won't record errors unless {Appsignal.active?} is `true`.
#
# @example
# # my_app.rb
# # setup AppSignal beforehand
#
# Appsignal.listen_for_error do
# # my code
# raise "foo"
# end
#
# @see Transaction.set_tags
# @see Transaction.set_namespace
# @see .send_error
# @see https://docs.appsignal.com/ruby/instrumentation/integrating-appsignal.html
# AppSignal integration guide
# @see https://docs.appsignal.com/ruby/instrumentation/exception-handling.html
# Exception handling guide
#
# @deprecated Use `rescue => error` with {.report_error} instead.
# @param tags [Hash, nil]
# @param namespace [String] the namespace for this error.
# @yield yields the given block.
# @return [Object] returns the return value of the given block.
def listen_for_error(
tags = nil,
namespace = Appsignal::Transaction::HTTP_REQUEST
)
stdout_and_logger_warning \
"The `Appsignal.listen_for_error` helper is deprecated. " \
"Please use `rescue => error` and `Appsignal.report_error` instead. " \
"Read our exception handling documentation: " \
"https://docs.appsignal.com/ruby/instrumentation/exception-handling.html"
yield
rescue Exception => error # rubocop:disable Lint/RescueException
send_error(error) do |transaction|
transaction.set_tags(tags) if tags
transaction.set_namespace(namespace) if namespace
end
raise error
end
alias :listen_for_exception :listen_for_error

# Send an error to AppSignal regardless of the context.
#
# Records and send the exception to AppSignal.
Expand Down
89 changes: 0 additions & 89 deletions spec/lib/appsignal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -678,24 +678,6 @@ def on_start
context "not active" do
before { Appsignal._config = project_fixture_config("not_active") }

describe ".listen_for_error" do
let(:error) { ExampleException.new("specific error") }

it "reraises the error" do
expect do
Appsignal.listen_for_error { raise error }
end.to raise_error(error)
end

it "does not create a transaction" do
expect do
expect do
Appsignal.listen_for_error { raise error }
end.to raise_error(error)
end.to_not(change { created_transactions.count })
end
end

describe ".send_error" do
let(:error) { ExampleException.new("specific error") }

Expand Down Expand Up @@ -1307,77 +1289,6 @@ def on_start
end
end

describe ".listen_for_error" do
around { |example| keep_transactions { example.run } }

it "prints and logs a deprecation warning" do
err_stream = std_stream
logs =
capture_logs do
capture_std_streams(std_stream, err_stream) do
Appsignal.listen_for_error do
# Do nothing
end
end
end
expect(err_stream.read)
.to include("appsignal WARNING: The `Appsignal.listen_for_error` helper is deprecated.")
expect(logs).to contains_log(
:warn,
"The `Appsignal.listen_for_error` helper is deprecated."
)
end

it "records the error and re-raise it" do
expect do
expect do
Appsignal.listen_for_error do
raise ExampleException, "I am an exception"
end
end.to raise_error(ExampleException, "I am an exception")
end.to change { created_transactions.count }.by(1)

# Default namespace
expect(last_transaction).to have_namespace(Appsignal::Transaction::HTTP_REQUEST)
expect(last_transaction).to have_error("ExampleException", "I am an exception")
expect(last_transaction).to_not include_tags
end

context "with tags" do
it "adds tags to the transaction" do
expect do
expect do
Appsignal.listen_for_error("foo" => "bar") do
raise ExampleException, "I am an exception"
end
end.to raise_error(ExampleException, "I am an exception")
end.to change { created_transactions.count }.by(1)

# Default namespace
expect(last_transaction).to have_namespace(Appsignal::Transaction::HTTP_REQUEST)
expect(last_transaction).to have_error("ExampleException", "I am an exception")
expect(last_transaction).to include_tags("foo" => "bar")
end
end

context "with a custom namespace" do
it "adds the namespace to the transaction" do
expect do
expect do
Appsignal.listen_for_error(nil, "custom_namespace") do
raise ExampleException, "I am an exception"
end
end.to raise_error(ExampleException, "I am an exception")
end.to change { created_transactions.count }.by(1)

# Default namespace
expect(last_transaction).to have_namespace("custom_namespace")
expect(last_transaction).to have_error("ExampleException", "I am an exception")
expect(last_transaction).to_not include_tags
end
end
end

describe ".set_error" do
let(:err_stream) { std_stream }
let(:stderr) { err_stream.read }
Expand Down

0 comments on commit 7c23292

Please sign in to comment.