From c63bf9055e316cc81bc021d7f7d3717f121a0737 Mon Sep 17 00:00:00 2001 From: Nicholas Date: Fri, 18 Oct 2024 13:34:23 +0000 Subject: [PATCH] bug: handle deepl_ex errors (#354) --- config/runtime.exs | 3 +- .../translate/translate_api_service_impl.ex | 10 ++++-- .../translate_api_service_impl_test.exs | 36 +++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index 493a678..916692e 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -18,7 +18,8 @@ if config_env() == :prod do "token", "private_key", "private_key_id", - "service_account" + "service_account", + "authorization" ]} ], metadata: {:all_except, [:conn, :domain, :application]} diff --git a/lib/qrstorage/services/translate/translate_api_service_impl.ex b/lib/qrstorage/services/translate/translate_api_service_impl.ex index 5b2224f..c205ab6 100644 --- a/lib/qrstorage/services/translate/translate_api_service_impl.ex +++ b/lib/qrstorage/services/translate/translate_api_service_impl.ex @@ -28,11 +28,15 @@ defmodule Qrstorage.Services.Translate.TranslateApiServiceImpl do @impl TranslateApiService def translate(text, target_language) do case DeeplEx.translate(text, :DETECT, map_target_language(target_language)) do - {:ok, translation} -> + {:ok, %Tesla.Env{status: status, body: response_body}} -> + Logger.error("Error while translating text: status: #{status} message: #{response_body}") + {:error} + + {:ok, translation} when is_bitstring(translation) -> {:ok, translation} - {:error, message} -> - Logger.error("Error while translating text: #{target_language} message: #{message}") + _ -> + Logger.error("Error while translating text: #{target_language}") {:error} end end diff --git a/test/qrstorage/services/translate/translate_api_service_impl_test.exs b/test/qrstorage/services/translate/translate_api_service_impl_test.exs index 3f04cec..2474206 100644 --- a/test/qrstorage/services/translate/translate_api_service_impl_test.exs +++ b/test/qrstorage/services/translate/translate_api_service_impl_test.exs @@ -1,5 +1,6 @@ defmodule Qrstorage.Services.Translate.TranslateApiServiceImplTest do use ExUnit.Case, async: true + import ExUnit.CaptureLog alias Qrstorage.Services.Translate.TranslateApiServiceImpl @@ -8,4 +9,39 @@ defmodule Qrstorage.Services.Translate.TranslateApiServiceImplTest do assert TranslateApiServiceImpl.map_target_language(:de) == :DE end end + + describe "translate/2" do + test "that translate/2 translates text" do + Tesla.Mock.mock(fn + %{method: :post, url: "https://api.deepl.com/v2/translate"} -> + %Tesla.Env{status: 200, body: %{"translations" => [%{"text" => "Hallo Welt"}]}} + end) + + assert TranslateApiServiceImpl.translate("Hello World", :de) == {:ok, "Hallo Welt"} + end + + test "that translate/2 logs deepl errors" do + Tesla.Mock.mock(fn + %{method: :post, url: "https://api.deepl.com/v2/translate"} -> + %Tesla.Env{status: 500, headers: [{"authorization", "test"}], body: "Error while processing the request"} + end) + + {result, log} = with_log(fn -> TranslateApiServiceImpl.translate("Hello World", :de) end) + + assert result == {:error} + assert log =~ "[error]" + end + + test "that translate/2 handles responses that are not strings" do + Tesla.Mock.mock(fn + %{method: :post, url: "https://api.deepl.com/v2/translate"} -> + %{other: "format"} + end) + + {result, log} = with_log(fn -> TranslateApiServiceImpl.translate("Hello World", :de) end) + + assert result == {:error} + assert log =~ "[error]" + end + end end