|
| 1 | +# TODO: Oban requires Elixir 1.13+, remove this once we depend on that too. |
| 2 | +if Version.match?(System.version(), "~> 1.13") do |
| 3 | + defmodule Sentry.Integrations.Oban.ErrorReporterTest do |
| 4 | + use ExUnit.Case, async: true |
| 5 | + |
| 6 | + alias Sentry.Integrations.Oban.ErrorReporter |
| 7 | + |
| 8 | + defmodule MyWorker do |
| 9 | + use Oban.Worker |
| 10 | + |
| 11 | + @impl Oban.Worker |
| 12 | + def perform(%Oban.Job{}), do: :ok |
| 13 | + end |
| 14 | + |
| 15 | + describe "handle_event/4" do |
| 16 | + test "reports the correct error to Sentry" do |
| 17 | + # Any worker is okay here, this is just an easier way to get a job struct. |
| 18 | + job = |
| 19 | + %{"id" => "123", "entity" => "user", "type" => "delete"} |
| 20 | + |> MyWorker.new() |
| 21 | + |> Ecto.Changeset.apply_action!(:validate) |
| 22 | + |> Map.replace!(:unsaved_error, %{ |
| 23 | + reason: %RuntimeError{message: "oops"}, |
| 24 | + kind: :error, |
| 25 | + stacktrace: [] |
| 26 | + }) |
| 27 | + |
| 28 | + Sentry.Test.start_collecting() |
| 29 | + |
| 30 | + assert :ok = |
| 31 | + ErrorReporter.handle_event( |
| 32 | + [:oban, :job, :exception], |
| 33 | + %{}, |
| 34 | + %{job: job}, |
| 35 | + :no_config |
| 36 | + ) |
| 37 | + |
| 38 | + assert [event] = Sentry.Test.pop_sentry_reports() |
| 39 | + assert event.original_exception == %RuntimeError{message: "oops"} |
| 40 | + assert [%{stacktrace: %{frames: [stacktrace]}} = exception] = event.exception |
| 41 | + |
| 42 | + assert exception.type == "RuntimeError" |
| 43 | + assert exception.value == "oops" |
| 44 | + assert exception.mechanism.handled == true |
| 45 | + assert stacktrace.module == MyWorker |
| 46 | + |
| 47 | + assert stacktrace.function == |
| 48 | + "Sentry.Integrations.Oban.ErrorReporterTest.MyWorker.process/1" |
| 49 | + |
| 50 | + assert event.tags.oban_queue == "default" |
| 51 | + assert event.tags.oban_state == "available" |
| 52 | + assert event.tags.oban_worker == "Sentry.Integrations.Oban.ErrorReporterTest.MyWorker" |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | +end |
0 commit comments