From 15c8ba4adb04d070f754c4b64a5252ea95c9a044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Wed, 12 Jun 2024 18:37:08 +0200 Subject: [PATCH 1/4] Allow passing exceptions or `{kind, reason}` --- lib/error_tracker/integrations/phoenix.ex | 8 ++++---- lib/error_tracker/integrations/plug.ex | 2 +- lib/error_tracker/schemas/error.ex | 16 ++++++++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/error_tracker/integrations/phoenix.ex b/lib/error_tracker/integrations/phoenix.ex index 1bb577d..15099cc 100644 --- a/lib/error_tracker/integrations/phoenix.ex +++ b/lib/error_tracker/integrations/phoenix.ex @@ -27,15 +27,15 @@ defmodule ErrorTracker.Integrations.Phoenix do end def handle_event([:phoenix, :router_dispatch, :exception], _measurements, metadata, :no_config) do - {reason, stack} = + {reason, kind, stack} = case metadata do - %{reason: %Plug.Conn.WrapperError{reason: reason, stack: stack}} -> + %{reason: %Plug.Conn.WrapperError{reason: reason, kind: kindstack: stack}} -> {reason, stack} - %{reason: reason, stacktrace: stack} -> + %{kind: kind, reason: reason, stacktrace: stack} -> {reason, stack} end - PlugIntegration.report_error(metadata.conn, reason, stack) + PlugIntegration.report_error(metadata.conn, {reason, kind}, stack) end end diff --git a/lib/error_tracker/integrations/plug.ex b/lib/error_tracker/integrations/plug.ex index 6c56bbd..76362a4 100644 --- a/lib/error_tracker/integrations/plug.ex +++ b/lib/error_tracker/integrations/plug.ex @@ -62,7 +62,7 @@ defmodule ErrorTracker.Integrations.Plug do catch kind, reason -> stack = __STACKTRACE__ - unquote(__MODULE__).report_error(conn, reason, stack) + unquote(__MODULE__).report_error(conn, {kind, reason}, stack) :erlang.raise(kind, reason, stack) end diff --git a/lib/error_tracker/schemas/error.ex b/lib/error_tracker/schemas/error.ex index 6bcfbdf..62c3526 100644 --- a/lib/error_tracker/schemas/error.ex +++ b/lib/error_tracker/schemas/error.ex @@ -24,9 +24,21 @@ defmodule ErrorTracker.Error do def new(exception, stacktrace = %ErrorTracker.Stacktrace{}) do source = ErrorTracker.Stacktrace.source(stacktrace) + {kind, reason} = + case exception do + %{__struct__: struct} = ex when is_exception(ex) -> + {to_string(struct), Exception.message(ex)} + + {_kind, %{__struct__: struct} = ex} when is_exception(ex) -> + {to_string(struct), Exception.message(ex)} + + {kind, ex} -> + {to_string(kind), to_string(ex)} + end + params = [ - kind: "error", - reason: Exception.message(exception), + kind: to_string(kind), + reason: reason, source_line: "#{source.file}:#{source.line}", source_function: "#{source.module}.#{source.function}/#{source.arity}" ] From e61bc5d39b9524fc2fc4cf67f250a6a3152e757f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Wed, 12 Jun 2024 18:37:15 +0200 Subject: [PATCH 2/4] Improve dev server options --- dev.exs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dev.exs b/dev.exs index 437f027..b02d8e6 100644 --- a/dev.exs +++ b/dev.exs @@ -59,6 +59,7 @@ defmodule ErrorTrackerDevWeb.PageController do
Generate Router 404
Raise NoRouteError from a controller
Generate Exception
+
Generate Exit
""") end @@ -70,6 +71,10 @@ defmodule ErrorTrackerDevWeb.PageController do raise "This is a controller exception" end + def call(_conn, :exit) do + exit(:timeout) + end + defp content(conn, content) do conn |> put_resp_header("content-type", "text/html") @@ -100,6 +105,7 @@ defmodule ErrorTrackerDevWeb.Router do get "/", ErrorTrackerDevWeb.PageController, :index get "/noroute", ErrorTrackerDevWeb.PageController, :noroute get "/exception", ErrorTrackerDevWeb.PageController, :exception + get "/exit", ErrorTrackerDevWeb.PageController, :exit end end From c949d905c48ab7b8081a038a059d9226e57c606d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 18 Jun 2024 15:38:53 +0200 Subject: [PATCH 3/4] Add missing param --- lib/error_tracker/integrations/phoenix.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/error_tracker/integrations/phoenix.ex b/lib/error_tracker/integrations/phoenix.ex index 15099cc..dd32520 100644 --- a/lib/error_tracker/integrations/phoenix.ex +++ b/lib/error_tracker/integrations/phoenix.ex @@ -29,11 +29,11 @@ defmodule ErrorTracker.Integrations.Phoenix do def handle_event([:phoenix, :router_dispatch, :exception], _measurements, metadata, :no_config) do {reason, kind, stack} = case metadata do - %{reason: %Plug.Conn.WrapperError{reason: reason, kind: kindstack: stack}} -> - {reason, stack} + %{reason: %Plug.Conn.WrapperError{reason: reason, kind: kind, stack: stack}} -> + {reason, kind, stack} %{kind: kind, reason: reason, stacktrace: stack} -> - {reason, stack} + {reason, kind, stack} end PlugIntegration.report_error(metadata.conn, {reason, kind}, stack) From f8674c39e1c37f2b959427d249783a5a1df23fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20de=20Arriba?= Date: Tue, 18 Jun 2024 16:09:07 +0200 Subject: [PATCH 4/4] Apply review suggestions --- lib/error_tracker/schemas/error.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/error_tracker/schemas/error.ex b/lib/error_tracker/schemas/error.ex index 62c3526..828ee53 100644 --- a/lib/error_tracker/schemas/error.ex +++ b/lib/error_tracker/schemas/error.ex @@ -26,10 +26,10 @@ defmodule ErrorTracker.Error do {kind, reason} = case exception do - %{__struct__: struct} = ex when is_exception(ex) -> + %struct{} = ex when is_exception(ex) -> {to_string(struct), Exception.message(ex)} - {_kind, %{__struct__: struct} = ex} when is_exception(ex) -> + {_kind, %struct{} = ex} when is_exception(ex) -> {to_string(struct), Exception.message(ex)} {kind, ex} ->