Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Show more friendly error messages for Google, LibreTranslate #127

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ en:
failed: "The translator is unable to translate this language."
not_supported: "This language is not supported by the translator."
too_long: "This post is too long to be translated by the translator."
not_available: "The translator service is currently not available."

microsoft:
missing_token: "The translator was unable to retrieve a valid token."
missing_key: "No Azure Subscription Key provided."
not_in_group:
not_in_group:
user_not_in_group: "You don't belong to a group allowed to translate."
poster_not_in_group: "Post wasn't made by an user in a allowlisted group. If empty it is disabled."
6 changes: 5 additions & 1 deletion services/discourse_translator/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def self.result(url, body)
end

if response.status != 200
raise TranslatorError.new(body || response.inspect)
if body && body["error"]
raise TranslatorError.new(body["error"]["message"])
else
raise TranslatorError.new(response.inspect)
end
else
body["data"]
end
Expand Down
26 changes: 14 additions & 12 deletions services/discourse_translator/libretranslate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,24 @@ def self.translate(post)
def self.result(url, body)
body[:key] = access_token

response =
Excon.post(
url,
body: URI.encode_www_form(body),
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
},
)

body = nil
begin
response =
Excon.post(
url,
body: URI.encode_www_form(body),
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
},
)

body = JSON.parse(response.body)
rescue JSON::ParserError
status = response.status
rescue JSON::ParserError, Excon::Error::Socket, Excon::Error::Timeout
body = I18n.t("translator.not_available")
status = 500
end

if response.status != 200
if status != 200
raise TranslatorError.new(body || response.inspect)
else
body["data"]
Expand Down