Skip to content

Commit

Permalink
bug: handle deepl_ex errors (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwittstruck authored Oct 18, 2024
1 parent 895c8d0 commit c63bf90
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down
10 changes: 7 additions & 3 deletions lib/qrstorage/services/translate/translate_api_service_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Qrstorage.Services.Translate.TranslateApiServiceImplTest do
use ExUnit.Case, async: true
import ExUnit.CaptureLog

alias Qrstorage.Services.Translate.TranslateApiServiceImpl

Expand All @@ -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

0 comments on commit c63bf90

Please sign in to comment.