Skip to content

Commit a2a6dff

Browse files
committed
fix: gracefully handle errors while loading app configs
1 parent 75d5a07 commit a2a6dff

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

src/dynamic-routes/dynamic-routes.tsx

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,52 @@ function remapNavigationItems(
3939
}
4040

4141
async function fetchAppConfig(url: string) {
42-
const response = await fetch(url);
43-
if (!response.ok) {
44-
throw new Error(`Failed to fetch app config from ${url}`);
45-
}
46-
const body = await response.text();
47-
const out = parseAppConfigJson(body);
42+
try {
43+
const response = await fetch(url);
44+
if (!response.ok) {
45+
throw new Error(`Non-ok status code returned from app config: ${url}`);
46+
}
47+
const body = await response.text();
48+
const out = parseAppConfigJson(body);
4849

49-
if (out instanceof type.errors) {
50-
console.warn(`Failed to parse app config for URL ${url}:`, out);
51-
return null;
52-
}
50+
if (out instanceof type.errors) {
51+
console.warn(`Failed to parse app config for URL ${url}:`, out);
52+
return null;
53+
}
5354

54-
return {
55-
...out,
56-
navigation: remapNavigationItems(out.navigation, url),
57-
};
55+
return {
56+
...out,
57+
navigation: remapNavigationItems(out.navigation, url),
58+
};
59+
} catch (e) {
60+
throw new Error(`Error loading app config from ${url}: ${e}`);
61+
}
5862
}
5963

6064
export async function loadAppConfigs(urls: string[]) {
6165
const configs: Record<string, AppConfig> = {};
6266

63-
const results = await Promise.all(urls.map(fetchAppConfig));
67+
const results = await Promise.allSettled(urls.map(fetchAppConfig));
6468

6569
for (let i = 0; i < urls.length; i++) {
66-
const config = results[i];
67-
if (config) {
68-
configs[urls[i]] = config;
70+
const result = results[i];
71+
72+
if (result.status === "rejected") {
73+
console.error(
74+
`Failed to load app config from ${urls[i]}:`,
75+
result.reason,
76+
);
77+
continue;
78+
}
79+
80+
const config = result.value;
81+
82+
if (!config) {
83+
console.warn(`App config from ${urls[i]} is null, skipping.`);
84+
continue;
6985
}
86+
87+
configs[urls[i]] = config;
7088
}
7189

7290
return configs;

0 commit comments

Comments
 (0)