From f2f097e9ca8ad2fa8bb30ee508242d4d4388e91c Mon Sep 17 00:00:00 2001 From: Aleksei Matiushkin Date: Tue, 3 Dec 2024 06:54:22 +0100 Subject: [PATCH] Better handling of the wrong config for messenger --- examples/tm/config/config.exs | 7 ++++--- lib/telemetria/messenger.ex | 29 +++++++++++++++++++++++++++++ mix.exs | 2 +- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/examples/tm/config/config.exs b/examples/tm/config/config.exs index 4fb5165..31c9239 100644 --- a/examples/tm/config/config.exs +++ b/examples/tm/config/config.exs @@ -6,6 +6,7 @@ config :telemetria, events: [ [:tm, :f_to_c] ], - throttle: %{some_group: {1_000, :last}}, - # create a slack app and put URL here - messenger_channels: %{slack: {:slack, url: ""}} + throttle: %{some_group: {1_000, :last}} + +# create a slack app and put URL here +# messenger_channels: %{slack: {:slack, url: ""}} diff --git a/lib/telemetria/messenger.ex b/lib/telemetria/messenger.ex index 708f9a7..697fde3 100644 --- a/lib/telemetria/messenger.ex +++ b/lib/telemetria/messenger.ex @@ -41,6 +41,35 @@ defmodule Telemetria.Messenger do else: inspect(message, opts) do_post(message, impl, opts) + rescue + e in KeyError -> + require Logger + + message = + """ + One or more required options were missing for the call to the messenger implementation + in `telemetría` module attribute (or global config.) + + For now this is rescued and application can continue, but messenging from telemetria won’t work. + + Error: #{e.message}. + """ + + Logger.error(message) + + e in RuntimeError -> + require Logger + + message = + """ + The unexpected runtime error occured. Usually it means the config in `runtime.exs` + is diverged from what `telemetría` compliler saw. Please move all the config to the + compile-time configurations files since `telemetría` needs them during a compilation stage. + + Error: #{e.message} + """ + + Logger.error(message) end def post(message, impl, opts) when is_binary(message), diff --git a/mix.exs b/mix.exs index 04311a1..6180e65 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule Telemetria.MixProject do use Mix.Project @app :telemetria - @version "0.21.1" + @version "0.21.2" def project do [