Skip to content

Commit

Permalink
WIP Update DatabaseQuery.call error match
Browse files Browse the repository at this point in the history
WIP

Signed-off-by: Eddy Babetto <[email protected]>
  • Loading branch information
eddbbt committed May 22, 2024
1 parent 912b5cd commit d2e4f5a
Show file tree
Hide file tree
Showing 6 changed files with 484 additions and 143 deletions.
139 changes: 100 additions & 39 deletions apps/astarte_appengine_api/lib/astarte_appengine_api/device/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)}.",
Expand Down
18 changes: 9 additions & 9 deletions apps/astarte_appengine_api/lib/astarte_appengine_api/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d2e4f5a

Please sign in to comment.