[Update] UI: Add error handlings and show error notifications. adjust each localization for it.

This commit is contained in:
Sakamoto Shiina
2025-03-17 15:58:16 +09:00
parent 84a116291c
commit c82a89a7fb
7 changed files with 262 additions and 67 deletions

View File

@@ -0,0 +1,124 @@
import { useTranslation } from "react-i18next";
import {
useNotificationStatus,
} from "@logics_common";
import {
useMicRecordTimeout,
useMicPhraseTimeout,
useMicMaxWords,
useSpeakerRecordTimeout,
useSpeakerPhraseTimeout,
useSpeakerMaxWords,
useDeepLAuthKey,
} from "@logics_configs";
import { ui_configs } from "../ui_configs";
export const _useBackendErrorHandling = () => {
const { t } = useTranslation();
const { showNotification_Error } = useNotificationStatus();
const { updateMicRecordTimeout } = useMicRecordTimeout();
const { updateMicPhraseTimeout } = useMicPhraseTimeout();
const { updateMicMaxWords } = useMicMaxWords();
const { updateSpeakerRecordTimeout } = useSpeakerRecordTimeout();
const { updateSpeakerPhraseTimeout } = useSpeakerPhraseTimeout();
const { updateSpeakerMaxWords } = useSpeakerMaxWords();
const { updateDeepLAuthKey } = useDeepLAuthKey();
const errorHandling_Backend = ({message, data, endpoint}) => {
switch (message) {
case "No mic device detected":
showNotification_Error(t("common_error.no_device_mic"));
break;
case "No Speaker device detected":
showNotification_Error(t("common_error.no_device_speaker"));
break;
case "Mic energy threshold value is out of range":
showNotification_Error(t("common_error.threshold_invalid_value",
{ min: ui_configs.mic_threshold_min, max: ui_configs.mic_threshold_max },
));
break;
case "Speaker energy threshold value is out of range":
showNotification_Error(t("common_error.threshold_invalid_value",
{ min: ui_configs.speaker_threshold_min, max: ui_configs.speaker_threshold_max },
));
break;
case "CTranslate2 weight download error":
showNotification_Error(t("common_error.failed_download_weight_ctranslate2"));
break;
case "Whisper weight download error":
showNotification_Error(t("common_error.failed_download_weight_whisper"));
break;
case "Translation engine limit error":
showNotification_Error(t("common_error.translation_limit"));
break;
case "DeepL auth key length is not correct":
updateDeepLAuthKey(data);
showNotification_Error(t("common_error.deepl_auth_key_invalid_length"));
break;
case "Authentication failure of deepL auth key":
updateDeepLAuthKey(data);
showNotification_Error(t("common_error.deepl_auth_key_failed_authentication"));
break;
case "Mic record timeout value is out of range":
updateMicRecordTimeout(data);
showNotification_Error(
t("common_error.invalid_value_mic_record_timeout",
{ mic_phrase_timeout_label: t("config_page.transcription.mic_phrase_timeout.label") }
));
break;
case "Mic phrase timeout value is out of range":
updateMicPhraseTimeout(data);
showNotification_Error(
t("common_error.invalid_value_mic_phrase_timeout",
{ mic_record_timeout_label: t("config_page.transcription.mic_record_timeout.label") }
));
break;
case "Mic max phrases value is out of range":
updateMicMaxWords(data);
showNotification_Error(t("common_error.invalid_value_mic_max_phrase"));
break;
case "Speaker record timeout value is out of range":
updateSpeakerRecordTimeout(data);
showNotification_Error(
t("common_error.invalid_value_speaker_record_timeout",
{ speaker_phrase_timeout_label: t("config_page.transcription.speaker_phrase_timeout.label") }
));
break;
case "Speaker phrase timeout value is out of range":
updateSpeakerPhraseTimeout(data);
showNotification_Error(
t("common_error.invalid_value_speaker_phrase_timeout",
{ speaker_record_timeout_label: t("config_page.transcription.speaker_record_timeout.label") }
));
break;
case "Speaker max phrases value is out of range":
updateSpeakerMaxWords(data);
showNotification_Error(t("common_error.invalid_value_speaker_max_phrase"));
break;
default:
if (endpoint === "/set/data/deepl_auth_key") updateDeepLAuthKey(data);
showNotification_Error(message);
break;
}
}
return {
errorHandling_Backend,
}
};

View File

@@ -1,6 +1,8 @@
import { translator_status } from "@ui_configs";
import { arrayToObject } from "@utils";
import { _useBackendErrorHandling } from "./_useBackendErrorHandling";
import {
useIsVrctAvailable,
useNotificationStatus,
@@ -184,6 +186,10 @@ export const useReceiveRoutes = () => {
const { handleNetworkConnection } = useHandleNetworkConnection();
const {
errorHandling_Backend,
} = _useBackendErrorHandling();
const routes = {
// Common
"/run/feed_watchdog": () => {},
@@ -498,16 +504,25 @@ export const useReceiveRoutes = () => {
"/get/data/transcription_engines": ()=>{}, // Not implemented on UI yet. (if ai_models has not been detected, this will be blank array[]. if the ai_models are ok but just network has not connected, it'l be only ["Whisper"])
};
const error_routes = {
"/set/data/mic_record_timeout": updateMicRecordTimeout,
"/set/data/mic_phrase_timeout": updateMicPhraseTimeout,
"/set/data/mic_max_phrases": updateMicMaxWords,
const error_status_routes = {
"/run/error_device": errorHandling_Backend,
"/set/data/speaker_record_timeout": updateSpeakerRecordTimeout,
"/set/data/speaker_phrase_timeout": updateSpeakerPhraseTimeout,
"/set/data/speaker_max_phrases": updateSpeakerMaxWords,
"/run/error_ctranslate2_weight": errorHandling_Backend,
"/run/error_whisper_weight": errorHandling_Backend,
"/set/data/deepl_auth_key": updateDeepLAuthKey,
"/set/data/deepl_auth_key": errorHandling_Backend,
"/run/error_translation_engine": errorHandling_Backend,
"/set/data/mic_threshold": errorHandling_Backend,
"/set/data/mic_record_timeout": errorHandling_Backend,
"/set/data/mic_phrase_timeout": errorHandling_Backend,
"/set/data/mic_max_phrases": errorHandling_Backend,
"/set/data/speaker_threshold": errorHandling_Backend,
"/set/data/speaker_record_timeout": errorHandling_Backend,
"/set/data/speaker_phrase_timeout": errorHandling_Backend,
"/set/data/speaker_max_phrases": errorHandling_Backend,
};
@@ -519,22 +534,37 @@ export const useReceiveRoutes = () => {
}
};
const handleInvalidEndpoint = (parsed_data) => {
console.error(`Invalid endpoint: ${parsed_data.endpoint}\nresult: ${JSON.stringify(parsed_data.result)}`);
};
if (parsed_data.endpoint === "/run/initialization_complete") {
initDataSyncProcess(parsed_data.result);
updateIsBackendReady(true);
return;
};
switch (parsed_data.status) {
case 200:
if (parsed_data.endpoint === "/run/initialization_complete") {
initDataSyncProcess(parsed_data.result);
updateIsBackendReady(true);
break;
};
const route = routes[parsed_data.endpoint];
(route) ? route(parsed_data.result) : console.error(`Invalid endpoint: ${parsed_data.endpoint}\nresult: ${JSON.stringify(parsed_data.result)}`);
if (route) {
route(parsed_data.result);
} else {
handleInvalidEndpoint(parsed_data);
}
break;
case 400:
const error_route = error_routes[parsed_data.endpoint];
(error_route) ? error_route(parsed_data.result.data) : console.error(`Invalid endpoint: ${parsed_data.endpoint}\nresult: ${JSON.stringify(parsed_data.result)}`);
console.error(`status 400: ${JSON.stringify(parsed_data.result)}`);
showNotification_Error(parsed_data.result.message);
const error_route = error_status_routes[parsed_data.endpoint];
if (error_route) {
error_route({
message: parsed_data.result.message,
data: parsed_data.result.data,
endpoint: parsed_data.endpoint,
});
} else {
handleInvalidEndpoint(parsed_data);
}
break;
case 348: