|
1 | 1 | defmodule Sentry.Integrations.Oban.ErrorReporterTest do |
2 | 2 | use ExUnit.Case, async: true |
3 | 3 |
|
| 4 | + import ExUnit.CaptureLog |
| 5 | + |
4 | 6 | alias Sentry.Integrations.Oban.ErrorReporter |
5 | 7 |
|
6 | 8 | defmodule MyWorker do |
@@ -210,6 +212,105 @@ defmodule Sentry.Integrations.Oban.ErrorReporterTest do |
210 | 212 | assert [event] = Sentry.Test.pop_sentry_reports() |
211 | 213 | assert event.tags.custom_tag == "custom_value" |
212 | 214 | end |
| 215 | + |
| 216 | + test "should_report_error_callback skips when callback returns false" do |
| 217 | + job = |
| 218 | + %{"id" => "123", "entity" => "user", "type" => "delete"} |
| 219 | + |> MyWorker.new() |
| 220 | + |> Ecto.Changeset.apply_action!(:validate) |
| 221 | + |
| 222 | + reason = %RuntimeError{message: "oops"} |
| 223 | + |
| 224 | + Sentry.Test.start_collecting() |
| 225 | + |
| 226 | + job_attempt_1 = Map.merge(job, %{attempt: 1, max_attempts: 3}) |
| 227 | + |
| 228 | + # Callback returns false -> skip reporting |
| 229 | + assert :ok = |
| 230 | + ErrorReporter.handle_event( |
| 231 | + [:oban, :job, :exception], |
| 232 | + %{}, |
| 233 | + %{job: job_attempt_1, kind: :error, reason: reason, stacktrace: []}, |
| 234 | + should_report_error_callback: fn _worker, job -> |
| 235 | + job.attempt >= job.max_attempts |
| 236 | + end |
| 237 | + ) |
| 238 | + |
| 239 | + assert [] = Sentry.Test.pop_sentry_reports() |
| 240 | + |
| 241 | + # Final attempt: callback returns true -> report |
| 242 | + job_attempt_3 = Map.merge(job, %{attempt: 3, max_attempts: 3}) |
| 243 | + |
| 244 | + assert :ok = |
| 245 | + ErrorReporter.handle_event( |
| 246 | + [:oban, :job, :exception], |
| 247 | + %{}, |
| 248 | + %{job: job_attempt_3, kind: :error, reason: reason, stacktrace: []}, |
| 249 | + should_report_error_callback: fn _worker, job -> |
| 250 | + job.attempt >= job.max_attempts |
| 251 | + end |
| 252 | + ) |
| 253 | + |
| 254 | + assert [event] = Sentry.Test.pop_sentry_reports() |
| 255 | + assert event.original_exception == %RuntimeError{message: "oops"} |
| 256 | + assert event.tags.oban_worker == "Sentry.Integrations.Oban.ErrorReporterTest.MyWorker" |
| 257 | + end |
| 258 | + |
| 259 | + test "should_report_error_callback receives worker module and job" do |
| 260 | + job = |
| 261 | + %{"id" => "123", "entity" => "user", "type" => "delete"} |
| 262 | + |> MyWorker.new() |
| 263 | + |> Ecto.Changeset.apply_action!(:validate) |
| 264 | + |
| 265 | + reason = %RuntimeError{message: "oops"} |
| 266 | + test_pid = self() |
| 267 | + |
| 268 | + Sentry.Test.start_collecting() |
| 269 | + |
| 270 | + assert :ok = |
| 271 | + ErrorReporter.handle_event( |
| 272 | + [:oban, :job, :exception], |
| 273 | + %{}, |
| 274 | + %{job: job, kind: :error, reason: reason, stacktrace: []}, |
| 275 | + should_report_error_callback: fn worker, received_job -> |
| 276 | + send(test_pid, {:callback_args, worker, received_job}) |
| 277 | + true |
| 278 | + end |
| 279 | + ) |
| 280 | + |
| 281 | + assert_receive {:callback_args, worker, received_job} |
| 282 | + assert worker == MyWorker |
| 283 | + assert received_job == job |
| 284 | + end |
| 285 | + |
| 286 | + test "should_report_error_callback reports when callback returns true" do |
| 287 | + Sentry.Test.start_collecting() |
| 288 | + |
| 289 | + emit_telemetry_for_failed_job(:error, %RuntimeError{message: "oops"}, [], |
| 290 | + should_report_error_callback: fn _worker, _job -> true end |
| 291 | + ) |
| 292 | + |
| 293 | + assert [event] = Sentry.Test.pop_sentry_reports() |
| 294 | + assert event.original_exception == %RuntimeError{message: "oops"} |
| 295 | + end |
| 296 | + |
| 297 | + test "should_report_error_callback handles errors gracefully and defaults to reporting" do |
| 298 | + Sentry.Test.start_collecting() |
| 299 | + |
| 300 | + log = |
| 301 | + capture_log(fn -> |
| 302 | + emit_telemetry_for_failed_job(:error, %RuntimeError{message: "oops"}, [], |
| 303 | + should_report_error_callback: fn _worker, _job -> raise "callback error" end |
| 304 | + ) |
| 305 | + end) |
| 306 | + |
| 307 | + assert log =~ "should_report_error_callback failed" |
| 308 | + assert log =~ "Sentry.Integrations.Oban.ErrorReporterTest.MyWorker" |
| 309 | + assert log =~ "callback error" |
| 310 | + |
| 311 | + assert [event] = Sentry.Test.pop_sentry_reports() |
| 312 | + assert event.original_exception == %RuntimeError{message: "oops"} |
| 313 | + end |
213 | 314 | end |
214 | 315 |
|
215 | 316 | ## Helpers |
|
0 commit comments