Commit 07aa17bb2c6d7a70c679bf6240e5e852db139515

Thomas de Grivel 2022-02-03T12:11:29

discord errors format

diff --git a/lib/discord.ex b/lib/discord.ex
index 3674137..ee1615a 100644
--- a/lib/discord.ex
+++ b/lib/discord.ex
@@ -2,7 +2,14 @@ defmodule Discord do
 
   def error(params) do
     url = Application.get_env(:kmxgit, :discord_errors_webhook)
-    message = %{content: "```#{inspect(params)}```"}
+    reason = if params.reason[:message] do
+      type = params.reason.__struct__
+      "#{type}: #{params.reason.message}"
+    else
+      inspect(params.reason)
+    end
+    stack = Stack.to_string(stack)
+    message = %{content: "```#{params.kind} #{reason}\n\n#{stack}```"}
     json = Jason.encode!(message)
     HTTPoison.post(url, json, [{"Content-Type", "application/json"}])
   end
diff --git a/lib/stack.ex b/lib/stack.ex
new file mode 100644
index 0000000..a280d7c
--- /dev/null
+++ b/lib/stack.ex
@@ -0,0 +1,7 @@
+defmodule Stack do
+
+  def to_string(stack), do: to_string(stack, [])
+
+  def to_string([], acc), do: acc |> Enum.reverse() |> Enum.join("\n")
+  def to_string([elt | rest], acc), do: to_string(rest, [inspect(elt) | acc])
+end