From d2e4f5ac72c50f9e054d39872489fe86238187b3 Mon Sep 17 00:00:00 2001 From: Eddy Babetto Date: Wed, 22 May 2024 12:21:19 +0200 Subject: [PATCH] WIP Update DatabaseQuery.call error match WIP Signed-off-by: Eddy Babetto --- .../astarte_appengine_api/device/queries.ex | 139 +++++--- .../lib/astarte_appengine_api/queries.ex | 18 +- .../astarte_appengine_api/rooms/queries.ex | 12 +- .../data_updater/queries.ex | 119 ++++--- .../lib/astarte_realm_management/queries.ex | 330 +++++++++++++++--- .../astarte_trigger_engine/events_consumer.ex | 9 + 6 files changed, 484 insertions(+), 143 deletions(-) diff --git a/apps/astarte_appengine_api/lib/astarte_appengine_api/device/queries.ex b/apps/astarte_appengine_api/lib/astarte_appengine_api/device/queries.ex index 1c68e4406..0a6b95d8d 100644 --- a/apps/astarte_appengine_api/lib/astarte_appengine_api/device/queries.ex +++ b/apps/astarte_appengine_api/lib/astarte_appengine_api/device/queries.ex @@ -66,7 +66,13 @@ defmodule Astarte.AppEngine.API.Device.Queries do {:error, :device_not_found} {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -246,7 +252,13 @@ defmodule Astarte.AppEngine.API.Device.Queries do {:error, :database_error} {:error, reason} -> - Logger.warning("Failed with reason: #{inspect(reason)}.") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -754,12 +766,14 @@ defmodule Astarte.AppEngine.API.Device.Queries do :empty_dataset -> {:error, :device_not_found} - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -784,12 +798,14 @@ defmodule Astarte.AppEngine.API.Device.Queries do :empty_dataset -> false - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - false - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") false end end @@ -889,6 +905,16 @@ defmodule Astarte.AppEngine.API.Device.Queries do :empty_dataset -> {:error, :device_not_found} + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") + {:error, :database_error} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :database_error} @@ -918,7 +944,13 @@ defmodule Astarte.AppEngine.API.Device.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -960,19 +992,27 @@ defmodule Astarte.AppEngine.API.Device.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end else nil -> {:error, :attribute_key_not_found} - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1029,12 +1069,14 @@ defmodule Astarte.AppEngine.API.Device.Queries do {:error, :device_not_found} -> {:error, :device_not_found} - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1094,12 +1136,14 @@ defmodule Astarte.AppEngine.API.Device.Queries do with {:ok, _result} <- DatabaseQuery.call(client, delete_batch) do :ok else - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end else @@ -1118,12 +1162,14 @@ defmodule Astarte.AppEngine.API.Device.Queries do nil -> {:error, :alias_tag_not_found} - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1158,12 +1204,14 @@ defmodule Astarte.AppEngine.API.Device.Queries do with {:ok, _result} <- DatabaseQuery.call(client, query) do :ok else - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Update failed, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Update failed, reason: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1272,6 +1320,19 @@ defmodule Astarte.AppEngine.API.Device.Queries do min(count, limit) else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Can't retrieve count for #{inspect(count_query)}: #{error_message}.", + tag: "db_error" + ) + + nil + error -> _ = Logger.warning("Can't retrieve count for #{inspect(count_query)}: #{inspect(error)}.", diff --git a/apps/astarte_appengine_api/lib/astarte_appengine_api/queries.ex b/apps/astarte_appengine_api/lib/astarte_appengine_api/queries.ex index 92b9318e4..f1ee33fdd 100644 --- a/apps/astarte_appengine_api/lib/astarte_appengine_api/queries.ex +++ b/apps/astarte_appengine_api/lib/astarte_appengine_api/queries.ex @@ -73,16 +73,16 @@ defmodule Astarte.AppEngine.API.Queries do {:ok, _result} <- DatabaseQuery.call(client, realms_query) do :ok else - %{acc: _, msg: err_msg} -> - _ = Logger.warning("Health is not good: #{err_msg}.", tag: "db_health_check_bad") + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end - {:error, :health_check_bad} - - {:error, err} -> - _ = - Logger.warning("Health is not good, reason: #{inspect(err)}.", - tag: "db_health_check_bad" - ) + Logger.warning("Health is not good, reason: #{error_message}.", + tag: "db_health_check_bad" + ) {:error, :health_check_bad} end diff --git a/apps/astarte_appengine_api/lib/astarte_appengine_api/rooms/queries.ex b/apps/astarte_appengine_api/lib/astarte_appengine_api/rooms/queries.ex index 022169390..416b0d264 100644 --- a/apps/astarte_appengine_api/lib/astarte_appengine_api/rooms/queries.ex +++ b/apps/astarte_appengine_api/lib/astarte_appengine_api/rooms/queries.ex @@ -41,12 +41,14 @@ defmodule Astarte.AppEngine.API.Rooms.Queries do :empty_dataset -> {:ok, false} - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error, reason: #{error_message}.", tag: "db_error") {:error, :database_error} end end diff --git a/apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/queries.ex b/apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/queries.ex index 9f4bda328..f090a5b63 100644 --- a/apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/queries.ex +++ b/apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/queries.ex @@ -74,13 +74,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do with {:ok, _result} <- DatabaseQuery.call(db_client, update_pending) do :ok else - %{acc: _, msg: error_message} -> - Logger.warning("Database error: #{error_message}.") - {:error, :database_error} - {:error, reason} -> - # DB Error - Logger.warning("Failed with reason #{inspect(reason)}.") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error, reason: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -380,7 +381,16 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do :ok else {:error, reason} -> - Logger.warning("Error while upserting path: #{path} (reason: #{inspect(reason)}).") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Error while upserting path: #{path} (reason:#{error_message}).", + tag: "db_error" + ) + {:error, :database_error} end end @@ -531,12 +541,17 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do ["ttl(connected)": nil] -> {:ok, 0} - %{acc: _, msg: error_message} -> - Logger.warning("Database error: #{error_message}.") - {:error, :database_error} - {:error, reason} -> - Logger.warning("Database error while retrieving property: #{inspect(reason)}.") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error while retrieving property: #{error_message}).", + tag: "db_error" + ) + {:error, :database_error} end end @@ -596,12 +611,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do [introspection_minor: nil] -> {:ok, %{}} - %{acc: _, msg: error_message} -> - Logger.warning("Database error: #{error_message}.") - {:error, :database_error} - {:error, reason} -> - Logger.warning("Failed with reason #{inspect(reason)}.") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Failed with reason #{error_message}).", tag: "db_error") {:error, :database_error} end end @@ -626,12 +643,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do [groups: nil] -> {:ok, []} - %{acc: _, msg: error_message} -> - Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - Logger.warning("Failed with reason #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Failed with reason #{error_message}).", tag: "db_error") {:error, :database_error} end end @@ -723,8 +742,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + Logger.warning( - "Database error: cannot register device-interface pair, reason: #{inspect(reason)}." + "Database error: cannot register device-interface pair, reason: #{error_message}." ) {:error, reason} @@ -751,8 +776,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + Logger.warning( - "Database error: cannot unregister device-interface pair: #{inspect(reason)}." + "Database error: cannot unregister device-interface pair: #{error_message}." ) {:error, reason} @@ -778,12 +809,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do :empty_dataset -> {:ok, false} - %{acc: _, msg: error_message} -> - _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") - {:error, :database_error} - {:error, reason} -> - _ = Logger.warning("Database error, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error, reason: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -890,12 +923,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do :empty_dataset -> {:ok, nil} - %{acc: _, msg: error_message} -> - Logger.warning("Database error: #{error_message}.") - {:error, :database_error} - {:error, reason} -> - Logger.warning("Failed with reason: #{inspect(reason)}.") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error, reason: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -935,12 +970,18 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Queries do ["ttl(datetime_value)": nil] -> {:ok, :no_expiry} - %{acc: _, msg: error_message} -> - Logger.warning("Database error: #{error_message}.") - {:error, :database_error} - {:error, reason} -> - Logger.warning("Database error while retrieving property: #{inspect(reason)}.") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = + Logger.warning("Database error while retrieving property: #{error_message}.", + tag: "db_error" + ) + {:error, :database_error} end end diff --git a/apps/astarte_realm_management/lib/astarte_realm_management/queries.ex b/apps/astarte_realm_management/lib/astarte_realm_management/queries.ex index b737f1e85..2a3a8ec1d 100644 --- a/apps/astarte_realm_management/lib/astarte_realm_management/queries.ex +++ b/apps/astarte_realm_management/lib/astarte_realm_management/queries.ex @@ -166,18 +166,14 @@ defmodule Astarte.RealmManagement.Queries do with {:ok, _result} <- DatabaseQuery.call(client, batch) do :ok else - %{acc: _, msg: error_message} -> - _ = - Logger.warning("Failed batch due to database error: #{error_message}.", tag: "db_error") - - {:error, :database_error} - {:error, reason} -> - _ = - Logger.warning("Failed batch due to database error: #{inspect(reason)}.", - tag: "db_error" - ) + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + Logger.warning("Failed batch due to database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -192,21 +188,17 @@ defmodule Astarte.RealmManagement.Queries do with {:ok, _result} <- DatabaseQuery.call(client, query) do {:cont, :ok} else - %{acc: _, msg: err_msg} -> - _ = - Logger.warning( - "Failed due to database error: #{err_msg}. Changes will not be undone!", - tag: "db_error" - ) - - {:halt, {:error, :database_error}} + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end - {:error, err} -> - _ = - Logger.warning( - "Failed due to database error: #{inspect(err)}. Changes will not be undone!", - tag: "db_error" - ) + Logger.warning( + "Failed batch due to database error: #{error_message}. Changes will not be undone!", + tag: "db_error" + ) {:halt, {:error, :database_error}} end @@ -248,9 +240,15 @@ defmodule Astarte.RealmManagement.Queries do {:error, :health_check_bad} - {:error, err} -> + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = - Logger.warning("Health is not good, reason: #{inspect(err)}.", tag: "health_check_bad") + Logger.warning("Health is not good, reason: #{error_message}.", tag: "health_check_bad") {:error, :health_check_bad} end @@ -469,7 +467,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -512,9 +516,15 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = Logger.error( - "Database error while deleting #{interface_name}, reason: #{inspect(reason)}.", + "Database error while deleting #{interface_name}, reason: #{error_message}.", tag: "db_error" ) @@ -536,7 +546,13 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -572,7 +588,13 @@ defmodule Astarte.RealmManagement.Queries do {:ok, false} {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -602,7 +624,13 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -633,8 +661,14 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = - Logger.warning("Database error: cannot delete values. Reason: #{inspect(reason)}.", + Logger.warning("Database error: cannot delete values. Reason: #{error_message}.", tag: "db_error" ) @@ -698,8 +732,14 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = - Logger.warning("Database error: cannot delete path values. Reason: #{inspect(reason)}.", + Logger.warning("Database error: cannot delete path values. Reason: #{error_message}.", tag: "db_error" ) @@ -756,8 +796,14 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = - Logger.warning("Database error while deleting all paths: #{inspect(reason)}.", + Logger.warning("Database error while deleting all paths: #{error_message}.", tag: "db_error" ) @@ -790,7 +836,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -818,7 +870,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -909,7 +967,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1015,7 +1079,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Failed, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1050,11 +1120,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = - Logger.warning("Database error: failed with reason: #{inspect(reason)}.", - tag: "db_error" - ) + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1086,7 +1158,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Failed with reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end @@ -1146,6 +1224,16 @@ defmodule Astarte.RealmManagement.Queries do {:ok, _res} <- DatabaseQuery.call(client, insert_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_install_trigger} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_install_trigger} @@ -1195,6 +1283,16 @@ defmodule Astarte.RealmManagement.Queries do {:ok, _res} <- DatabaseQuery.call(client, insert_simple_trigger_by_uuid_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_install_simple_trigger} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_install_simple_trigger} @@ -1229,6 +1327,16 @@ defmodule Astarte.RealmManagement.Queries do {:ok, _result} <- DatabaseQuery.call(client, insert_trigger_to_policy_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_install_trigger_policy_link} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_install_trigger_policy_link} @@ -1257,6 +1365,16 @@ defmodule Astarte.RealmManagement.Queries do :empty_dataset -> {:error, :trigger_not_found} + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_retrieve_trigger_uuid} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_retrieve_trigger_uuid} @@ -1289,6 +1407,16 @@ defmodule Astarte.RealmManagement.Queries do {:ok, _result} <- DatabaseQuery.call(client, delete_trigger_to_policy_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_delete_trigger_policy_link} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_delete_trigger_policy_link} @@ -1317,6 +1445,16 @@ defmodule Astarte.RealmManagement.Queries do {:ok, _result} <- DatabaseQuery.call(client, delete_trigger_by_name_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_delete_trigger} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_delete_trigger} @@ -1334,6 +1472,16 @@ defmodule Astarte.RealmManagement.Queries do trigger[:key] end else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_list_triggers} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_list_triggers} @@ -1359,6 +1507,16 @@ defmodule Astarte.RealmManagement.Queries do :empty_dataset -> {:error, :trigger_not_found} + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_retrieve_trigger} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_retrieve_trigger} @@ -1397,6 +1555,20 @@ defmodule Astarte.RealmManagement.Queries do } } else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = + Logger.warning("Possible inconsistency found: database error: #{error_message}.", + tag: "db_error" + ) + + {:error, :cannot_retrieve_simple_trigger} + not_ok -> _ = Logger.warning("Possible inconsistency found: database error: #{inspect(not_ok)}.", @@ -1444,6 +1616,16 @@ defmodule Astarte.RealmManagement.Queries do {:ok, _result} <- DatabaseQuery.call(client, delete_astarte_ref_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_delete_simple_trigger} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_delete_simple_trigger} @@ -1467,6 +1649,16 @@ defmodule Astarte.RealmManagement.Queries do :empty_dataset -> {:error, :trigger_not_found} + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_retrieve_simple_trigger} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") @@ -1487,6 +1679,16 @@ defmodule Astarte.RealmManagement.Queries do with {:ok, _res} <- DatabaseQuery.call(client, insert_query) do :ok else + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") + {:error, :cannot_install_trigger_policy} + not_ok -> _ = Logger.warning("Database error: #{inspect(not_ok)}.", tag: "db_error") {:error, :cannot_install_trigger_policy} @@ -1517,11 +1719,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = - Logger.warning("Database error: failed with reason: #{inspect(reason)}.", - tag: "db_error" - ) + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") {:error, :database_error} end end @@ -1553,7 +1757,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Failed, reason: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{inspect(error_message)}.", tag: "db_error") {:error, :database_error} end end @@ -1575,9 +1785,15 @@ defmodule Astarte.RealmManagement.Queries do {:ok, false} {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = Logger.error( - "Database error while checking #{policy_name}, reason: #{inspect(reason)}.", + "Database error while checking #{policy_name}, reason: #{error_message}.", tag: "db_error" ) @@ -1624,9 +1840,15 @@ defmodule Astarte.RealmManagement.Queries do :ok else {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + _ = Logger.error( - "Database error while deleting #{policy_name}, reason: #{inspect(reason)}.", + "Database error while deleting #{policy_name}, reason: #{error_message}.", tag: "db_error" ) @@ -1660,7 +1882,13 @@ defmodule Astarte.RealmManagement.Queries do {:error, :database_error} {:error, reason} -> - _ = Logger.warning("Database error: #{inspect(reason)}.", tag: "db_error") + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") {:error, :database_error} end end diff --git a/apps/astarte_trigger_engine/lib/astarte_trigger_engine/events_consumer.ex b/apps/astarte_trigger_engine/lib/astarte_trigger_engine/events_consumer.ex index 456116338..a30d34067 100644 --- a/apps/astarte_trigger_engine/lib/astarte_trigger_engine/events_consumer.ex +++ b/apps/astarte_trigger_engine/lib/astarte_trigger_engine/events_consumer.ex @@ -330,6 +330,15 @@ defmodule Astarte.TriggerEngine.EventsConsumer do Logger.warning("Database connection error.") {:error, :database_connection_error} + {:error, reason} -> + error_message = + case reason do + %{acc: _, msg: error_msg} -> error_msg + _ -> inspect(reason) + end + + _ = Logger.warning("Database error: #{error_message}.", tag: "db_error") + {:error, :database_connection_error} error -> Logger.warning("Error while processing event: #{inspect(error)}") {:error, :trigger_not_found}