prevent usage of combinedSettings if endpoints fail (which none of them should) (#2201)

This commit is contained in:
rkuo-danswer 2024-08-21 18:27:38 -07:00 committed by GitHub
parent 020dff52f7
commit e50b558b5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 15 deletions

View File

@ -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 (
<html lang="en">
<Head>
<title>Settings Unavailable</title>
</Head>
<body>
<div className="error">
Settings could not be loaded. Please try again later.
</div>
</body>
</html>
);
}
return (
<html lang="en">

View File

@ -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;
}
}