Skip to content

Commit 247b890

Browse files
authored
FIX: Suppress errors when translating (#299)
There are two errors here being handled - Random finaldestination lookup errors when hitting the API. This is not automatically caught at the translate method on purpose, but rather at the usages. - validation errors when updating locale. Sometimes posts are created bypassing validations, when updating the locale, we need to bypass validation as well else the locale can never be updated.
1 parent 6bd2738 commit 247b890

File tree

7 files changed

+20
-1
lines changed

7 files changed

+20
-1
lines changed

app/jobs/regular/detect_translate_post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def execute(args)
2020

2121
begin
2222
DiscourseTranslator::PostTranslator.translate(post, locale)
23+
rescue FinalDestination::SSRFDetector::LookupFailedError
24+
# do nothing, there are too many sporadic lookup failures
2325
rescue => e
2426
Rails.logger.error(
2527
"Discourse Translator: Failed to translate post #{post.id} to #{locale}: #{e.message}",

app/jobs/regular/detect_translate_topic.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def execute(args)
2222

2323
begin
2424
DiscourseTranslator::TopicTranslator.translate(topic, locale)
25+
rescue FinalDestination::SSRFDetector::LookupFailedError
26+
# do nothing, there are too many sporadic lookup failures
2527
rescue => e
2628
Rails.logger.error(
2729
"Discourse Translator: Failed to translate topic #{topic.id} to #{locale}: #{e.message}",

app/jobs/regular/translate_categories.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def execute(args)
2626
next if CategoryLocalization.exists?(category_id: category.id, locale: locale)
2727
begin
2828
DiscourseTranslator::CategoryTranslator.translate(category, locale)
29+
rescue FinalDestination::SSRFDetector::LookupFailedError
30+
# do nothing, there are too many sporadic lookup failures
2931
rescue => e
3032
Rails.logger.error(
3133
"Discourse Translator: Failed to translate category #{category.id} to #{locale}: #{e.message}",

app/jobs/regular/translate_posts.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def execute(args)
3535
posts.each do |post|
3636
begin
3737
DiscourseTranslator::PostTranslator.translate(post, locale)
38+
rescue FinalDestination::SSRFDetector::LookupFailedError
39+
# do nothing, there are too many sporadic lookup failures
3840
rescue => e
3941
Rails.logger.error(
4042
"Discourse Translator: Failed to translate post #{post.id} to #{locale}: #{e.message}",

app/jobs/regular/translate_topics.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def execute(args)
3434
topics.each do |topic|
3535
begin
3636
DiscourseTranslator::TopicTranslator.translate(topic, locale)
37+
rescue FinalDestination::SSRFDetector::LookupFailedError
38+
# do nothing, there are too many sporadic lookup failures
3739
rescue => e
3840
Rails.logger.error(
3941
"Discourse Translator: Failed to translate topic #{topic.id} to #{locale}: #{e.message}",

app/services/discourse_translator/post_locale_detector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def self.detect_locale(post)
88
translator = DiscourseTranslator::Provider::TranslatorProvider.get
99
detected_locale = translator.detect!(post)
1010
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
11-
post.update!(locale:)
11+
post.update_column(:locale, locale)
1212
locale
1313
end
1414
end

spec/services/post_locale_detector_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@
2323
"zh_CN",
2424
)
2525
end
26+
27+
it "bypasses validations when updating locale" do
28+
post.update_column(:raw, "A")
29+
30+
translator.stubs(:detect!).with(post).returns("zh_CN")
31+
32+
described_class.detect_locale(post)
33+
expect(post.reload.locale).to eq("zh_CN")
34+
end
2635
end
2736
end

0 commit comments

Comments
 (0)