From ccd8d2b6b02f22898c2c4a4397e2d341e7a8236e Mon Sep 17 00:00:00 2001 From: jeffvli Date: Thu, 16 Nov 2023 23:35:26 -0800 Subject: [PATCH] Add network error catch --- src/i18n/locales/en.json | 1 + src/renderer/api/jellyfin/jellyfin-api.ts | 9 +++++++++ src/renderer/api/navidrome/navidrome-api.ts | 14 +++++++++++--- src/renderer/api/subsonic/subsonic-api.ts | 10 ++++++++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index e0b3eb77..3997bd64 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -144,6 +144,7 @@ "localFontAccessDenied": "access denied to local fonts", "loginRateError": "too many login attempts, please try again in a few seconds", "mpvRequired": "MPV required", + "networkError": "a network error occurred", "playbackError": "an error occurred when trying to play the media", "remoteDisableError": "an error occurred when trying to $t(common.disable) the remote server", "remoteEnableError": "an error occurred when trying to $t(common.enable) the remote server", diff --git a/src/renderer/api/jellyfin/jellyfin-api.ts b/src/renderer/api/jellyfin/jellyfin-api.ts index 7ce4b6c3..3303c742 100644 --- a/src/renderer/api/jellyfin/jellyfin-api.ts +++ b/src/renderer/api/jellyfin/jellyfin-api.ts @@ -7,6 +7,7 @@ import { ServerListItem } from '/@/renderer/types'; import omitBy from 'lodash/omitBy'; import { z } from 'zod'; import { authenticationFailure } from '/@/renderer/api/utils'; +import i18n from '/@/i18n/i18n'; const c = initContract(); @@ -337,6 +338,14 @@ export const jfApiClient = (args: { }; } catch (e: Error | AxiosError | any) { if (isAxiosError(e)) { + if (e.code === 'ERR_NETWORK') { + throw new Error( + i18n.t('error.networkError', { + postProcess: 'sentenceCase', + }) as string, + ); + } + const error = e as AxiosError; const response = error.response as AxiosResponse; return { diff --git a/src/renderer/api/navidrome/navidrome-api.ts b/src/renderer/api/navidrome/navidrome-api.ts index 25cb1939..04cef24a 100644 --- a/src/renderer/api/navidrome/navidrome-api.ts +++ b/src/renderer/api/navidrome/navidrome-api.ts @@ -380,12 +380,20 @@ export const ndApiClient = (args: { }; } catch (e: Error | AxiosError | any) { if (isAxiosError(e)) { + if (e.code === 'ERR_NETWORK') { + throw new Error( + i18n.t('error.networkError', { + postProcess: 'sentenceCase', + }) as string, + ); + } + const error = e as AxiosError; const response = error.response as AxiosResponse; return { - body: { data: response.data, headers: response.headers }, - headers: response.headers as any, - status: response.status, + body: { data: response?.data, headers: response?.headers }, + headers: response?.headers as any, + status: response?.status, }; } throw e; diff --git a/src/renderer/api/subsonic/subsonic-api.ts b/src/renderer/api/subsonic/subsonic-api.ts index 858172f9..5a620f19 100644 --- a/src/renderer/api/subsonic/subsonic-api.ts +++ b/src/renderer/api/subsonic/subsonic-api.ts @@ -185,9 +185,15 @@ export const ssApiClient = (args: { status: result.status, }; } catch (e: Error | AxiosError | any) { - console.log('CATCH ERR'); - if (isAxiosError(e)) { + if (e.code === 'ERR_NETWORK') { + throw new Error( + i18n.t('error.networkError', { + postProcess: 'sentenceCase', + }) as string, + ); + } + const error = e as AxiosError; const response = error.response as AxiosResponse;