Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error storage data #79

Merged
merged 9 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule ExampleAppWeb.PageController do
end

def check_custom_notifier(conn, _params) do
errors = ExampleApp.MemoryErrorStorage.get_error()
render(conn, "check_custom_notifier.html", errors: errors)
error = ExampleApp.MemoryErrorStorage.get_error()
render(conn, "check_custom_notifier.html", error: error)
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<%= for error <- @errors do %>
<pre class="error">
<%= inspect(error) %>
</pre>
<hr>
<% end %>
<pre class="error">
<%= inspect(@error) %>
</pre>
65 changes: 32 additions & 33 deletions example_app/test/send_notification_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,31 @@ defmodule ExampleAppWeb.SendNotificationTest do
end

feature "Sends notification in groups", %{session: session} do
for %{expected_notifications: expected_notifications, expected_emails: expected_emails} <-
for %{accumulated_occurrences: accumulated_occurrences, expected_emails: expected_emails} <-
[
%{expected_notifications: 1, expected_emails: 1},
%{expected_notifications: 1, expected_emails: 1},
%{expected_notifications: 2, expected_emails: 2},
%{expected_notifications: 2, expected_emails: 2},
%{expected_notifications: 2, expected_emails: 2},
%{expected_notifications: 2, expected_emails: 2},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 4, expected_emails: 3},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 4},
%{expected_notifications: 8, expected_emails: 5}
%{accumulated_occurrences: 1, expected_emails: 1},
%{accumulated_occurrences: 1, expected_emails: 1},
%{accumulated_occurrences: 2, expected_emails: 2},
%{accumulated_occurrences: 2, expected_emails: 2},
%{accumulated_occurrences: 2, expected_emails: 2},
%{accumulated_occurrences: 2, expected_emails: 2},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 4, expected_emails: 3},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 4},
%{accumulated_occurrences: 8, expected_emails: 5}
] do
session
|> visit("/group-exception")
Expand All @@ -94,15 +94,14 @@ defmodule ExampleAppWeb.SendNotificationTest do
|> find(Query.css(".list-group"))
|> find(Query.css(".list-group-item", count: expected_emails))

item = if expected_emails == 1, do: items, else: List.first(items)
if expected_emails > 1 do
item = List.first(items)
email = select_email(session, item)

email_page = select_email(session, item)

email_body_sections =
text(email_page, Query.css(".body-text"))
|> String.split("----------------------------------------", trim: true)

assert(length(email_body_sections) == expected_notifications)
email
|> find(Query.css(".body-text"))
|> assert_text("Errors: #{accumulated_occurrences}")
end
end
end

Expand All @@ -115,7 +114,7 @@ defmodule ExampleAppWeb.SendNotificationTest do
|> visit("/check-custom-notifier")
|> assert_has(
Query.css(".error",
text: "name: CustomNotifierExceptionError, reason: \"custom notifier exception error\""
text: "name: CustomNotifierExceptionError"
)
)

Expand Down
14 changes: 8 additions & 6 deletions lib/boom_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule BoomNotifier do
# Responsible for sending a notification to each notifier every time an
# exception is raised.

alias BoomNotifier.ErrorInfo
alias BoomNotifier.ErrorStorage
alias BoomNotifier.NotifierSenderServer
require Logger
Expand Down Expand Up @@ -85,22 +86,23 @@ defmodule BoomNotifier do

defp do_notify_error(conn, settings, error) do
{custom_data, _settings} = Keyword.pop(settings, :custom_data, :nothing)
{error_kind, error_info} = ErrorInfo.build(error, conn, custom_data)
error_info = ErrorInfo.build(error, conn, custom_data)

ErrorStorage.add_errors(error_kind, error_info)
ErrorStorage.store_error(error_info)

if ErrorStorage.send_notification?(error_kind) do
occurrences = ErrorStorage.get_errors(error_kind)
if ErrorStorage.send_notification?(error_info) do
notification_data =
Map.put(error_info, :occurrences, ErrorStorage.get_error_stats(error_info))

# Triggers the notification in each notifier
walkthrough_notifiers(settings, fn notifier, options ->
NotifierSenderServer.send(notifier, occurrences, options)
NotifierSenderServer.send(notifier, notification_data, options)
end)

{notification_trigger, _settings} =
Keyword.pop(settings, :notification_trigger, :always)

ErrorStorage.clear_errors(notification_trigger, error_kind)
ErrorStorage.reset_accumulated_errors(notification_trigger, error_info)
end
end
end
Expand Down
30 changes: 17 additions & 13 deletions lib/boom_notifier/error_info.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule ErrorInfo do
defmodule BoomNotifier.ErrorInfo do
@moduledoc false

# The ErrorInfo struct holds all the information about the exception.
Expand All @@ -7,9 +7,19 @@ defmodule ErrorInfo do
# among other things) and custom data depending on the configuration.

@enforce_keys [:reason, :stack, :timestamp]
defstruct [:name, :reason, :stack, :controller, :action, :request, :timestamp, :metadata]

@type t :: %ErrorInfo{}
defstruct [
:name,
:reason,
:stack,
:controller,
:action,
:request,
:timestamp,
:metadata,
:occurrences
]

@type t :: %__MODULE__{}

@type option ::
:logger
Expand All @@ -26,11 +36,11 @@ defmodule ErrorInfo do
},
map(),
custom_data_strategy_type
) :: {atom(), ErrorInfo.t()}
def build(%{reason: reason, stack: stack} = error, conn, custom_data_strategy) do
) :: __MODULE__.t()
def build(%{reason: reason, stack: stack}, conn, custom_data_strategy) do
{error_reason, error_name} = error_reason(reason)

error_info = %ErrorInfo{
%__MODULE__{
reason: error_reason,
stack: stack,
controller: get_in(conn.private, [:phoenix_controller]),
Expand All @@ -40,19 +50,13 @@ defmodule ErrorInfo do
name: error_name,
metadata: build_custom_data(conn, custom_data_strategy)
}

{error_type(error), error_info}
end

defp error_reason(%name{message: reason}), do: {reason, name}
defp error_reason(%{message: reason}), do: {reason, "Error"}
defp error_reason(reason) when is_binary(reason), do: error_reason(%{message: reason})
defp error_reason(reason), do: error_reason(%{message: inspect(reason)})

defp error_type(%{reason: %name{}}), do: name
defp error_type(%{error: %{kind: kind}}), do: kind
defp error_type(_), do: :error

defp build_request_info(conn) do
%{
path: conn.request_path,
Expand Down
Loading