Skip to content

Commit

Permalink
Use add_error in report_error helper
Browse files Browse the repository at this point in the history
Within the `report_error` helper, use `add_error` instead of the
`set_error` method, so that multiple errors can be reported from
the same transaction.
  • Loading branch information
unflxw committed Jul 26, 2024
1 parent 008f600 commit 70ffc00
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .changesets/support-adding-multiple-errors-to-a-transaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
bump: patch
type: add
---

Support adding multiple errors to a transaction.

Using the `Appsignal.report_error` helper, you can now report more than one error within the same transaction context, up to a maximum of ten errors per transaction. Each error will be reported as a separate sample in the AppSignal UI.

Before this change, using `Appsignal.report_error` or `Appsignal.set_error` helpers, adding a new error within the same transaction would overwrite the previous one.
4 changes: 2 additions & 2 deletions lib/appsignal/helpers/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def set_error(exception)
# @since 3.10.0
def report_error(exception)
unless exception.is_a?(Exception)
internal_logger.error "Appsignal.report_error: Cannot set error. " \
internal_logger.error "Appsignal.report_error: Cannot add error. " \
"The given value is not an exception: #{exception.inspect}"
return
end
Expand All @@ -379,7 +379,7 @@ def report_error(exception)
)
end

transaction.set_error(exception)
transaction.add_error(exception)
yield transaction if block_given?

return if has_parent_transaction
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/appsignal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ def on_start
logs = capture_logs { Appsignal.report_error(error) }
expect(logs).to contains_log(
:error,
"Appsignal.report_error: Cannot set error. " \
"Appsignal.report_error: Cannot add error. " \
"The given value is not an exception: #{error.inspect}"
)
end
Expand Down Expand Up @@ -1392,7 +1392,7 @@ def on_start
expect(last_transaction).to eq(transaction)
transaction._sample
expect(transaction).to have_namespace(Appsignal::Transaction::HTTP_REQUEST)
expect(transaction).to have_error("ExampleException", "error message")
expect(transaction.errors).to eq([error])
end

it "does not complete the transaction" do
Expand All @@ -1412,7 +1412,7 @@ def on_start
transaction._sample
expect(transaction).to have_namespace("my_namespace")
expect(transaction).to have_action("my_action")
expect(transaction).to have_error("ExampleException", "error message")
expect(transaction.errors).to eq([error])
expect(transaction).to include_tags("tag1" => "value1")
expect(transaction).to_not be_completed
end
Expand Down

0 comments on commit 70ffc00

Please sign in to comment.