Skip to content

Commit 260f8bf

Browse files
authored
Ensure only valid responses are processed (#292)
1 parent 627ca96 commit 260f8bf

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

lib/utils.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,32 @@ export const getAppConfig = cache(async (headers: Headers): Promise<AppConfig> =
3838
headers: { 'X-Sandbox-ID': sandboxId },
3939
});
4040

41-
const remoteConfig: SandboxConfig = await response.json();
42-
const config: AppConfig = { ...APP_CONFIG_DEFAULTS, sandboxId };
41+
if (response.ok) {
42+
const remoteConfig: SandboxConfig = await response.json();
4343

44-
for (const [key, entry] of Object.entries(remoteConfig)) {
45-
if (entry === null) continue;
46-
// Only include app config entries that are declared in defaults and, if set,
47-
// share the same primitive type as the default value.
48-
if (
49-
(key in APP_CONFIG_DEFAULTS &&
50-
APP_CONFIG_DEFAULTS[key as keyof AppConfig] === undefined) ||
51-
(typeof config[key as keyof AppConfig] === entry.type &&
52-
typeof config[key as keyof AppConfig] === typeof entry.value)
53-
) {
54-
// @ts-expect-error I'm not sure quite how to appease TypeScript, but we've thoroughly checked types above
55-
config[key as keyof AppConfig] = entry.value as AppConfig[keyof AppConfig];
44+
const config: AppConfig = { ...APP_CONFIG_DEFAULTS, sandboxId };
45+
46+
for (const [key, entry] of Object.entries(remoteConfig)) {
47+
if (entry === null) continue;
48+
// Only include app config entries that are declared in defaults and, if set,
49+
// share the same primitive type as the default value.
50+
if (
51+
(key in APP_CONFIG_DEFAULTS &&
52+
APP_CONFIG_DEFAULTS[key as keyof AppConfig] === undefined) ||
53+
(typeof config[key as keyof AppConfig] === entry.type &&
54+
typeof config[key as keyof AppConfig] === typeof entry.value)
55+
) {
56+
// @ts-expect-error I'm not sure quite how to appease TypeScript, but we've thoroughly checked types above
57+
config[key as keyof AppConfig] = entry.value as AppConfig[keyof AppConfig];
58+
}
5659
}
57-
}
5860

59-
return config;
61+
return config;
62+
} else {
63+
console.error(
64+
`ERROR: querying config endpoint failed with status ${response.status}: ${response.statusText}`
65+
);
66+
}
6067
} catch (error) {
6168
console.error('ERROR: getAppConfig() - lib/utils.ts', error);
6269
}

0 commit comments

Comments
 (0)