Catch the error

This commit is contained in:
Lim Chee Aun 2023-04-27 18:12:38 +08:00
parent 2ed6b80dcc
commit 23057efcca

View file

@ -54,28 +54,33 @@ function TranslationBlock({
const translate = async () => { const translate = async () => {
setUIState('loading'); setUIState('loading');
const { content, detectedSourceLanguage, provider, ...props } = try {
await onTranslate(apiSourceLang.current, targetLang); const { content, detectedSourceLanguage, provider, ...props } =
if (content) { await onTranslate(apiSourceLang.current, targetLang);
if (detectedSourceLanguage) { if (content) {
const detectedLangText = localeCode2Text(detectedSourceLanguage); if (detectedSourceLanguage) {
setDetectedLang(detectedLangText); const detectedLangText = localeCode2Text(detectedSourceLanguage);
} setDetectedLang(detectedLangText);
if (provider === 'lingva') {
const pronunciation = props?.info?.pronunciation?.query;
if (pronunciation) {
setPronunciationContent(pronunciation);
} }
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); } catch (e) {
setUIState('default'); console.error(e);
detailsRef.current.open = true;
detailsRef.current.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
} else {
console.error(result);
setUIState('error'); setUIState('error');
} }
}; };