From e50b558b5be138846e8a3d42489c804dfc359905 Mon Sep 17 00:00:00 2001 From: rkuo-danswer Date: Wed, 21 Aug 2024 18:27:38 -0700 Subject: [PATCH] prevent usage of combinedSettings if endpoints fail (which none of them should) (#2201) --- web/src/app/layout.tsx | 15 ++++++++ web/src/components/settings/lib.ts | 57 ++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/web/src/app/layout.tsx b/web/src/app/layout.tsx index c4b1b6fe5..0ce53edf2 100644 --- a/web/src/app/layout.tsx +++ b/web/src/app/layout.tsx @@ -49,6 +49,21 @@ export default async function RootLayout({ children: React.ReactNode; }) { const combinedSettings = await fetchSettingsSS(); + if (!combinedSettings) { + // Just display a simple full page error if fetching fails. + return ( + + + Settings Unavailable + + +
+ Settings could not be loaded. Please try again later. +
+ + + ); + } return ( diff --git a/web/src/components/settings/lib.ts b/web/src/components/settings/lib.ts index 35d287347..881ba8db2 100644 --- a/web/src/components/settings/lib.ts +++ b/web/src/components/settings/lib.ts @@ -31,22 +31,49 @@ export async function fetchSettingsSS() { } } - const results = await Promise.all(tasks); + try { + const results = await Promise.all(tasks); - const settings = (await results[0].json()) as Settings; - const enterpriseSettings = - tasks.length > 1 ? ((await results[1].json()) as EnterpriseSettings) : null; - const customAnalyticsScript = ( - tasks.length > 2 ? await results[2].json() : null - ) as string | null; - const webVersion = getWebVersion(); + if (!results[0].ok) { + throw new Error( + `fetchStandardSettingsSS failed: status=${results[0].status} body=${await results[0].text()}` + ); + } - const combinedSettings: CombinedSettings = { - settings, - enterpriseSettings, - customAnalyticsScript, - webVersion, - }; + const settings = await results[0].json(); - return combinedSettings; + let enterpriseSettings = null; + if (tasks.length > 1) { + if (!results[1].ok) { + throw new Error( + `fetchEnterpriseSettingsSS failed: status=${results[1].status} body=${await results[1].text()}` + ); + } + enterpriseSettings = (await results[1].json()) as EnterpriseSettings; + } + + let customAnalyticsScript = null; + if (tasks.length > 2) { + if (!results[2].ok) { + throw new Error( + `fetchCustomAnalyticsScriptSS failed: status=${results[2].status} body=${await results[2].text()}` + ); + } + customAnalyticsScript = (await results[2].json()) as string; + } + + const webVersion = getWebVersion(); + + const combinedSettings: CombinedSettings = { + settings, + enterpriseSettings, + customAnalyticsScript, + webVersion, + }; + + return combinedSettings; + } catch (error) { + console.error("fetchSettingsSS exception: ", error); + return null; + } }