From ac513edac8a21f0987bd6355f5d20a10449c51af Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 15 Jan 2025 20:41:47 +0100 Subject: [PATCH] update error messages and get guild name --- lib/mocks/guild.ex | 7 ++++ .../cogs/plantnet_message.ex | 37 ++++++++++++------- lib/plantid_discord_bot/guild.ex | 8 ++++ test/error_handling_test.exs | 8 ++++ test/test_helper.exs | 8 ++++ 5 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 lib/mocks/guild.ex create mode 100644 lib/plantid_discord_bot/guild.ex create mode 100644 test/error_handling_test.exs diff --git a/lib/mocks/guild.ex b/lib/mocks/guild.ex new file mode 100644 index 0000000..c524059 --- /dev/null +++ b/lib/mocks/guild.ex @@ -0,0 +1,7 @@ +defmodule PlantIdDiscordBotTest.Mocks.Guild do + @moduledoc """ + Mocks the Guild module. + """ + def get_guild_name!(guild_id), + do: %{guild_id: 1_002_507_312_159_797_318, guild_name: "Test Guild"} +end diff --git a/lib/plantid_discord_bot/cogs/plantnet_message.ex b/lib/plantid_discord_bot/cogs/plantnet_message.ex index e2a95a5..faef5ad 100644 --- a/lib/plantid_discord_bot/cogs/plantnet_message.ex +++ b/lib/plantid_discord_bot/cogs/plantnet_message.ex @@ -3,11 +3,9 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do use Nostrum.Consumer alias Nostrum.Api - alias PlantIdDiscordBot.RateLimiter + alias PlantIdDiscordBot.{Guild, RateLimiter, Metrics} alias PlantIdDiscordBot.FileServer.File alias PlantIdDiscordBot.PlantNet.Parser - alias Nostrum.Cache.GuildCache - alias PlantIdDiscordBot.Metrics # TODO reintroduce mocks # @api Application.compile_env(:plantid_discord_bot, :api) @@ -35,10 +33,16 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do |> Enum.map(fn attachment -> attachment.url end) |> File.download_and_save_files!() rescue - # TODO add guild id and name into error - # TODO test by raising error - e in ArgumentError -> - Api.create_message(message.channel_id, content: e.message) + e -> + Logger.error(Exception.format(:error, e, __STACKTRACE__), + guild_id: message.guild_id, + guild_name: Guild.get_guild_name!(message.guild_id) + ) + + Api.create_message(message.channel_id, + content: "An error has occured. Please try again later." + ) + nil end @@ -50,7 +54,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do |> get_response(message) rescue e -> - Logger.error(Exception.format(:error, e, __STACKTRACE__)) + Logger.error(Exception.format(:error, e, __STACKTRACE__), + guild_id: message.guild_id, + guild_name: Guild.get_guild_name!(message.guild_id) + ) Api.create_message( message.channel_id, @@ -76,9 +83,7 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do defp get_response(query_uri, message) do guild_id = message.guild_id - - # TODO move into its own function - {:ok, %{name: guild_name}} = GuildCache.get(guild_id) + guild_name = get_guild_name!(guild_id) case HTTPoison.get(query_uri) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> @@ -95,7 +100,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do ) {:ok, %HTTPoison.Response{status_code: 401, body: body}} -> - Logger.critical("Unauthorized request to PlantNet API: #{body}") + Logger.critical("Unauthorized request to PlantNet API: #{body}", + guild_id: guild_id, + guild_name: guild_name + ) Api.create_message(message.channel_id, content: "Unauthorized request to PlantNet API.", @@ -120,7 +128,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do ) {_, _} -> - Logger.error("Internal server error when contacting the PlantNet API") + Logger.error("Internal server error when contacting the PlantNet API", + guild_id: guild_id, + guild_name: guild_name + ) Api.create_message(message.channel_id, content: "Internal Server Error", diff --git a/lib/plantid_discord_bot/guild.ex b/lib/plantid_discord_bot/guild.ex new file mode 100644 index 0000000..d7cff98 --- /dev/null +++ b/lib/plantid_discord_bot/guild.ex @@ -0,0 +1,8 @@ +defmodule PlantIdDiscordBot.Guild do + alias Nostrum.Cache.GuildCache + + def get_guild_name!(guild_id) do + %{name: guild_name} = GuildCache.get!(guild_id) + guild_name + end +end diff --git a/test/error_handling_test.exs b/test/error_handling_test.exs new file mode 100644 index 0000000..2880088 --- /dev/null +++ b/test/error_handling_test.exs @@ -0,0 +1,8 @@ +defmodule PlantIdDiscordBot.ErrorHandlingTest do + use ExUnit.Case + + test "do_identification/1 returns nil when an error occurs" do + message = PlantNetFixtures.Message.message() + PlantIdDiscordBot.Cog.PlantNetMessage.do_identification(message) + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index c6a7944..582b93e 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,13 @@ ExUnit.start() +defmodule PlantNetFixtures.Message do + @message %{ + guild_id: 1_002_507_312_159_797_318 + } + + def message(), do: @message +end + defmodule PlantNetFixtures do @plantnet_raw_response "{\"query\":{\"project\":\"all\",\"images\":[\"https://upload.wikimedia.org/wikipedia/commons/f/ff/Prunus_cerasifera_A.jpg\",\"https://le-jardin-de-pascal.com/2195113-large_default/prunus-cerasifera-atropurpurea-prunier-myrobolan-nigra.jpg\"],\"organs\":[\"auto\",\"auto\"],\"includeRelatedImages\":false,\"noReject\":false},\"language\":\"en\",\"preferedReferential\":\"k-world-flora\",\"bestMatch\":\"Prunus cerasifera Ehrh.\",\"results\":[{\"score\":0.87871,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus cerasifera\",\"scientificNameAuthorship\":\"Ehrh.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Cherry plum, myrobalan\",\"Cherry Plum\",\"Purple-leaf Plum\"],\"scientificName\":\"Prunus cerasifera Ehrh.\"},\"gbif\":{\"id\":\"3021730\"},\"powo\":{\"id\":\"729568-1\"},\"iucn\":{\"id\":\"172162\",\"category\":\"DD\"}},{\"score\":0.31668,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus × cistena\",\"scientificNameAuthorship\":\"N.E.Hansen ex Koehne\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Dwarf red-leaf plum\",\"Purple-leaf sand cherry\",\"Purple-leaved sand cherry\"],\"scientificName\":\"Prunus × cistena N.E.Hansen ex Koehne\"},\"gbif\":{\"id\":\"3022465\"},\"powo\":{\"id\":\"2959315-4\"}},{\"score\":0.01801,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus sargentii\",\"scientificNameAuthorship\":\"Rehder\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Sargent's cherry\",\"Northern Japanese hill cherry\",\"Sargent’s cherry\"],\"scientificName\":\"Prunus sargentii Rehder\"},\"gbif\":{\"id\":\"3020955\"},\"powo\":{\"id\":\"730239-1\"},\"iucn\":{\"id\":\"64127603\",\"category\":\"LC\"}},{\"score\":0.00896,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus × yedoensis\",\"scientificNameAuthorship\":\"Matsum.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Yoshino cherry\",\"Hybrid cherry\",\"Korean flowering cherry\"],\"scientificName\":\"Prunus × yedoensis Matsum.\"},\"gbif\":{\"id\":\"3021335\"},\"powo\":{\"id\":\"30119904-2\"}},{\"score\":0.00518,\"species\":{\"scientificNameWithoutAuthor\":\"Prunus serrulata\",\"scientificNameAuthorship\":\"Lindl.\",\"genus\":{\"scientificNameWithoutAuthor\":\"Prunus\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Prunus\"},\"family\":{\"scientificNameWithoutAuthor\":\"Rosaceae\",\"scientificNameAuthorship\":\"\",\"scientificName\":\"Rosaceae\"},\"commonNames\":[\"Japanese flowering cherry\",\"Japanese flowering cherry Kwanzan\",\"Tibetan Cherry\"],\"scientificName\":\"Prunus serrulata Lindl.\"},\"gbif\":{\"id\":\"3022609\"},\"powo\":{\"id\":\"730268-1\"},\"iucn\":{\"id\":\"217170511\",\"category\":\"LC\"}}],\"version\":\"2024-11-19 (7.3)\",\"remainingIdentificationRequests\":488}"