Skip to content

Commit

Permalink
chore: Reduce log level of TTS Service #337 (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwittstruck authored Sep 13, 2024
1 parent 4f81616 commit 71e0970
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
5 changes: 4 additions & 1 deletion lib/qrstorage/services/qr_code_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ defmodule Qrstorage.Services.QrCodeService do
case handle_types(qr_code, qr_code_params) do
{:error, error_message} ->
# delete qr code and show error
Logger.error("deleting qr code after creation, because the audio part could not be saved")
Logger.error(
"deleting qr code after creation, because the audio part could not be saved: #{error_message} #{qr_code.language} #{qr_code.voice}"
)

Repo.delete(qr_code)
{:error, QrCodes.change_qr_code(%QrCode{}), error_message}

Expand Down
32 changes: 22 additions & 10 deletions lib/qrstorage/services/tts/text_to_speech_api_service_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ defmodule Qrstorage.Services.Tts.TextToSpeechApiServiceImpl do

@impl TextToSpeechApiService
def text_to_audio(text, language, voice) do
data = %{
key: api_key(),
lang: TextToSpeechMapping.language_code(language),
voice: TextToSpeechMapping.voice(language, voice),
text: text
}
if text == nil || String.length(text) == 0 do
Logger.error("TTS Error: Text ist empty #{language} #{voice}")
{:error, "Error while creating text to speech transformation"}
else
data = %{
key: api_key(),
lang: TextToSpeechMapping.language_code(language),
voice: TextToSpeechMapping.voice(language, voice),
text: text
}

post_data(data)
post_data(data)
end
end

# build dynamic client based on runtime arguments
Expand All @@ -27,7 +32,7 @@ defmodule Qrstorage.Services.Tts.TextToSpeechApiServiceImpl do
Tesla.Middleware.FormUrlencoded,
# we need redirects, since the read speaker api redirects to the created .mp3 file
{Tesla.Middleware.FollowRedirects, max_redirects: 1},
{Tesla.Middleware.Logger, debug: false}
{Tesla.Middleware.Logger, debug: false, log_level: &custom_log_level/1}
]

Tesla.client(middleware)
Expand All @@ -38,8 +43,8 @@ defmodule Qrstorage.Services.Tts.TextToSpeechApiServiceImpl do
{:ok, %Tesla.Env{status: status, body: response_body}} when status in 200..201 ->
{:ok, response_body}

{:ok, %Tesla.Env{status: status}} ->
Logger.error("http error: #{status}")
{:ok, %Tesla.Env{status: status, body: response_body}} ->
Logger.error("http error: #{status} #{data[:lang]} #{data[:voice]} #{response_body}")
{:error, "Error while creating text to speech transformation"}

{:error, reason} ->
Expand All @@ -48,6 +53,13 @@ defmodule Qrstorage.Services.Tts.TextToSpeechApiServiceImpl do
end
end

defp custom_log_level(env) do
case env.status do
302 -> :info
_ -> :default
end
end

defp api_key() do
{:ok, api_key} = read_api_key()
api_key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ defmodule Qrstorage.Services.Tts.TextToSpeechApiServiceImplTest do
alias Qrstorage.Services.Tts.TextToSpeechApiServiceImpl

describe "text_to_audio/3" do
test "that text_to_audio/3 returns error with empty text" do
{result, log} = with_log(fn -> TextToSpeechApiServiceImpl.text_to_audio("", :de, "female") end)

assert result == {:error, "Error while creating text to speech transformation"}
assert log =~ "[error]"
end

test "that text_to_audio/3 returns error without text" do
{result, log} = with_log(fn -> TextToSpeechApiServiceImpl.text_to_audio(nil, :de, "female") end)

assert result == {:error, "Error while creating text to speech transformation"}
assert log =~ "[error]"
end

test "that text_to_audio/3 returns file" do
Tesla.Mock.mock(fn
%{method: :post, url: "https://scapi-eu.readspeaker.com/a/speak"} ->
Expand All @@ -17,14 +31,12 @@ defmodule Qrstorage.Services.Tts.TextToSpeechApiServiceImplTest do
test "that text_to_audio/3 logs error" do
Tesla.Mock.mock(fn
%{method: :post, url: "https://scapi-eu.readspeaker.com/a/speak"} ->
%Tesla.Env{status: 500, body: "Error while processig the request"}
%Tesla.Env{status: 500, body: "Error while processing the request"}
end)

{result, log} = with_log(fn -> TextToSpeechApiServiceImpl.text_to_audio("Hello World", :de, "female") end)

assert result ==
{:error, "Error while creating text to speech transformation"}

assert result == {:error, "Error while creating text to speech transformation"}
assert log =~ "[error]"
end
end
Expand Down

0 comments on commit 71e0970

Please sign in to comment.