Skip to content

Commit

Permalink
fix: error reporting on speech synthesis failures
Browse files Browse the repository at this point in the history
In the error case, this attempt at logging the error would instead crash
because something like `{:http_error, ...}` can't be directly
interpolated into a string. This fixes the logging, adds some context,
and adds a Sentry report so we'll be notified when this happens again
on a real screen.
  • Loading branch information
digitalcora committed Aug 19, 2024
1 parent f07d595 commit 46eeafe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
28 changes: 21 additions & 7 deletions lib/screens/audio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule Screens.Audio do

@type departure_group :: {departure_group_key(), [map()]}

def synthesize(ssml_string, is_screen, text_type) do
@spec synthesize(String.t(), String.t(), keyword()) :: {:ok, binary()} | :error
def synthesize(ssml_string, text_type, log_meta) do
result =
ssml_string
|> ExAws.Polly.synthesize_speech(lexicon_names: @lexicon_names, text_type: text_type)
Expand All @@ -30,16 +31,13 @@ defmodule Screens.Audio do
{:ok, %{body: audio_data}} ->
{:ok, audio_data}

{:error, reason} ->
_ =
if is_screen do
Logger.error("synthesize_ssml_failed #{reason}")
end

{:error, error} ->
report_error(ssml_string, error, log_meta)
:error
end
end

# used in V1 only
def from_api_data(
%{
station_name: station_name,
Expand Down Expand Up @@ -188,4 +186,20 @@ defmodule Screens.Audio do
psa_config -> Fetch.fetch_psa(psa_config)
end
end

defp report_error(ssml_string, error, meta) do
Logger.error(
"synthesize_ssml_failed string=#{inspect(ssml_string)} error=#{inspect(error)}",
meta
)

_ =
if meta[:is_screen] do
Sentry.capture_message("synthesize_ssml_failed",
extra: Enum.into(meta, %{error: error, string: ssml_string})
)
end

nil
end
end
3 changes: 2 additions & 1 deletion lib/screens_web/controllers/audio_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ defmodule ScreensWeb.AudioController do
Screens.ScreenData.by_screen_id(screen_id, is_screen, check_disabled: true),
template_assigns <- Screens.Audio.from_api_data(data, screen_id),
ssml <- render_ssml(template_assigns),
{:ok, audio_data} <- Screens.Audio.synthesize(ssml, is_screen, "ssml") do
{:ok, audio_data} <-
Screens.Audio.synthesize(ssml, "ssml", screen_id: screen_id, is_screen: is_screen) do
send_audio(conn, {:binary, audio_data}, disposition)
else
_ -> send_fallback_audio(conn, is_screen, screen_id, disposition)
Expand Down
4 changes: 2 additions & 2 deletions lib/screens_web/controllers/v2/audio_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule ScreensWeb.V2.AudioController do
end

def text_to_speech(conn, %{"text" => text}) do
case Screens.Audio.synthesize(text, false, "text") do
case Screens.Audio.synthesize(text, "text", is_screen: false) do
{:ok, audio_data} ->
send_download(conn, {:binary, audio_data}, filename: "readout.mp3", disposition: :inline)

Expand All @@ -60,7 +60,7 @@ defmodule ScreensWeb.V2.AudioController do
defp readout(conn, screen_id, real_screen?, disposition) do
screen_id
|> fetch_ssml()
|> Screens.Audio.synthesize(real_screen?, "ssml")
|> Screens.Audio.synthesize("ssml", screen_id: screen_id, is_screen: real_screen?)
|> case do
{:ok, audio_data} ->
send_download(conn, {:binary, audio_data},
Expand Down

0 comments on commit 46eeafe

Please sign in to comment.