From 23057efccad4ef3cfcd58759ca0cbdd9a253ef71 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 27 Apr 2023 18:12:38 +0800 Subject: [PATCH] Catch the error --- src/components/translation-block.jsx | 45 +++++++++++++++------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/components/translation-block.jsx b/src/components/translation-block.jsx index ff35c12c..5183af03 100644 --- a/src/components/translation-block.jsx +++ b/src/components/translation-block.jsx @@ -54,28 +54,33 @@ function TranslationBlock({ const translate = async () => { setUIState('loading'); - const { content, detectedSourceLanguage, provider, ...props } = - await onTranslate(apiSourceLang.current, targetLang); - if (content) { - if (detectedSourceLanguage) { - const detectedLangText = localeCode2Text(detectedSourceLanguage); - setDetectedLang(detectedLangText); - } - if (provider === 'lingva') { - const pronunciation = props?.info?.pronunciation?.query; - if (pronunciation) { - setPronunciationContent(pronunciation); + try { + const { content, detectedSourceLanguage, provider, ...props } = + await onTranslate(apiSourceLang.current, targetLang); + if (content) { + if (detectedSourceLanguage) { + const detectedLangText = localeCode2Text(detectedSourceLanguage); + setDetectedLang(detectedLangText); } + if (provider === 'lingva') { + const pronunciation = props?.info?.pronunciation?.query; + if (pronunciation) { + setPronunciationContent(pronunciation); + } + } + setTranslatedContent(content); + setUIState('default'); + detailsRef.current.open = true; + detailsRef.current.scrollIntoView({ + behavior: 'smooth', + block: 'nearest', + }); + } else { + console.error(result); + setUIState('error'); } - setTranslatedContent(content); - setUIState('default'); - detailsRef.current.open = true; - detailsRef.current.scrollIntoView({ - behavior: 'smooth', - block: 'nearest', - }); - } else { - console.error(result); + } catch (e) { + console.error(e); setUIState('error'); } };