diff --git a/README.md b/README.md index 49b5bf70..5cd88d37 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ defp deps do {:sentry, "~> 13.0"}, {:jason, "~> 1.4"}, - {:finch, "~> 0.17.0"} + {:finch, "~> 0.21"} ] end ``` diff --git a/lib/mix/tasks/sentry.install.ex b/lib/mix/tasks/sentry.install.ex index e931fb7c..5b42bbc4 100644 --- a/lib/mix/tasks/sentry.install.ex +++ b/lib/mix/tasks/sentry.install.ex @@ -43,7 +43,7 @@ if Code.ensure_loaded?(Igniter) do def info(_argv, _composing_task) do %Igniter.Mix.Task.Info{ group: :sentry, - adds_deps: [{:jason, "~> 1.2"}, {:hackney, ">= 1.8.0 and < 5.0.0"}], + adds_deps: [{:jason, "~> 1.4"}, {:finch, "~> 0.21"}], example: __MODULE__.Docs.example(), schema: [dsn: :string], defaults: [dsn: ""] @@ -63,7 +63,7 @@ if Code.ensure_loaded?(Igniter) do "prod.exs", :sentry, [:environment_name], - {:code, quote(do: Mix.env())} + {:code, quote(do: config_env())} ) |> Igniter.Project.Config.configure( "prod.exs", @@ -138,14 +138,24 @@ if Code.ensure_loaded?(Igniter) do end defp setup_endpoint(igniter, endpoint) do + # Sentry.PlugCapture is only recommended for Cowboy; on Bandit it can + # result in duplicate errors. + uses_cowboy? = Igniter.Project.Deps.has_dep?(igniter, :plug_cowboy) + Igniter.Project.Module.find_and_update_module!(igniter, endpoint, fn zipper -> zipper - |> Igniter.Code.Common.within(&add_plug_capture/1) + |> maybe_add_plug_capture(uses_cowboy?) |> Igniter.Code.Common.within(&add_plug_context/1) |> then(&{:ok, &1}) end) end + defp maybe_add_plug_capture(zipper, true) do + Igniter.Code.Common.within(zipper, &add_plug_capture/1) + end + + defp maybe_add_plug_capture(zipper, false), do: zipper + defp add_plug_capture(zipper) do with :error <- Igniter.Code.Module.move_to_use(zipper, Sentry.PlugCapture), {:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Phoenix.Endpoint) do diff --git a/test/mix/sentry.install_test.exs b/test/mix/sentry.install_test.exs index 4df061a9..07933276 100644 --- a/test/mix/sentry.install_test.exs +++ b/test/mix/sentry.install_test.exs @@ -37,7 +37,7 @@ defmodule Mix.Tasks.Sentry.InstallTest do ] end - test "installation adds jason and hackney dependencies", %{igniter: igniter} do + test "installation adds jason and finch dependencies", %{igniter: igniter} do igniter |> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"]) |> assert_creates("config/prod.exs", """ @@ -45,14 +45,11 @@ defmodule Mix.Tasks.Sentry.InstallTest do config :sentry, dsn: "test_dsn", - environment_name: Mix.env(), + environment_name: config_env(), enable_source_code_context: true, root_source_code_paths: [File.cwd!()] """) |> assert_has_patch("lib/test_web/endpoint.ex", """ - + | use Sentry.PlugCapture - """) - |> assert_has_patch("lib/test_web/endpoint.ex", """ + | plug(Sentry.PlugContext) """) |> assert_has_patch("lib/test/application.ex", """ @@ -62,6 +59,28 @@ defmodule Mix.Tasks.Sentry.InstallTest do """) end + test "installation does not add Sentry.PlugCapture without plug_cowboy", %{igniter: igniter} do + endpoint = + igniter + |> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"]) + |> apply_igniter!() + |> then(& &1.assigns[:test_files]["lib/test_web/endpoint.ex"]) + + refute endpoint =~ "Sentry.PlugCapture" + end + + test "installation adds Sentry.PlugCapture when the project uses plug_cowboy", %{ + igniter: igniter + } do + igniter + |> Igniter.Project.Deps.add_dep({:plug_cowboy, "~> 2.7"}) + |> apply_igniter!() + |> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"]) + |> assert_has_patch("lib/test_web/endpoint.ex", """ + + | use Sentry.PlugCapture + """) + end + test "installation is idempotent", %{igniter: igniter} do igniter |> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])