Hardened root layout initialization and ghost fallback logic to resolve 500 errors during API downtime.

This commit is contained in:
Scott Idem
2026-01-16 16:51:11 -05:00
parent a10accfaaf
commit 24ccf38412
4 changed files with 73 additions and 66 deletions

View File

@@ -107,21 +107,23 @@ export async function load({ fetch, params, parent, route, url }) {
const fqdn = url.host;
let result: ae_SiteDomain | null = null;
let result: any = null;
try {
if (log_lvl) console.log(`ROOT LOAD: Starting site lookup for ${fqdn}...`);
result = await lookup_site_domain({
api_cfg: ae_api_init,
fqdn,
view: 'base',
log_lvl
});
if (log_lvl) console.log('ROOT LOAD: Site lookup result:', result);
} catch (err) {
console.error('Site lookup critical failure in root layout.', err);
console.error('ROOT LOAD: Site lookup critical failure.', err);
}
if (result === null) {
console.warn('Site lookup returned null. Attempting emergency ghost fallback.');
// This is a last resort if the internal library fallback also failed
// Defensive check: if result is false (common from API helper) or null, use emergency ghost
if (!result || typeof result !== 'object') {
console.warn('ROOT LOAD: Result was falsy or non-object. Forcing ghost fallback.');
result = {
id: 'ghost',
id_random: 'ghost',
@@ -131,41 +133,39 @@ export async function load({ fetch, params, parent, route, url }) {
site_id_random: 'ghost',
site_domain_id_random: 'ghost',
enable: '1',
header_image_path: '',
style_href: '',
google_tracking_id: '',
access_code_kv_json: {},
cfg_json: {},
access_key: '',
site_domain_access_key: ''
} as any;
style_href: '',
header_image_path: ''
};
}
const json_data = result as any;
account_id = json_data.account_id_random;
data_struct.account_id = json_data.account_id_random;
ae_acct.account_id = json_data.account_id_random;
const json_data = result;
// CRITICAL: SvelteKit hydration can fail if these are undefined
account_id = json_data.account_id_random || 'ghost';
data_struct.account_id = account_id;
ae_acct.account_id = account_id;
ae_api_init['account_id'] = json_data.account_id_random;
ae_api_init['headers']['x-account-id'] = json_data.account_id_random;
// ae_api_init['headers']['x-no-account-id'] = null;
if (log_lvl) console.log(`ROOT LOAD: Using account_id: ${account_id}`);
ae_api_headers['x-account-id'] = ae_account_id;
ae_api_init['account_id'] = account_id;
ae_api_init['headers']['x-account-id'] = account_id;
ae_loc_init['account_id'] = json_data.account_id_random;
ae_loc_init['account_code'] = json_data.account_code;
ae_loc_init['account_name'] = json_data.account_name;
ae_api_headers['x-account-id'] = account_id;
ae_loc_init['site_id'] = json_data.site_id_random;
ae_loc_init['site_domain_id'] = json_data.site_domain_id_random;
ae_loc_init['site_enable'] = json_data.enable;
ae_loc_init['site_header_image_path'] = json_data.header_image_path;
ae_loc_init['site_style_href'] = json_data.style_href;
ae_loc_init['site_google_tracking_id'] = json_data.google_tracking_id;
ae_loc_init['site_access_code_kv'] = json_data.access_code_kv_json;
ae_loc_init['site_cfg_json'] = json_data.cfg_json;
ae_loc_init['site_access_key'] = json_data.access_key;
ae_loc_init['site_domain_access_key'] = json_data.site_domain_access_key;
ae_loc_init['account_id'] = account_id;
ae_loc_init['account_code'] = json_data.account_code || 'ghost';
ae_loc_init['account_name'] = json_data.account_name || 'Ghost Account';
ae_loc_init['site_id'] = json_data.site_id_random || 'ghost';
ae_loc_init['site_domain_id'] = json_data.site_domain_id_random || 'ghost';
ae_loc_init['site_enable'] = json_data.enable || '1';
ae_loc_init['site_header_image_path'] = json_data.header_image_path || '';
ae_loc_init['site_style_href'] = json_data.style_href || '';
ae_loc_init['site_google_tracking_id'] = json_data.google_tracking_id || '';
ae_loc_init['site_access_code_kv'] = json_data.access_code_kv_json || {};
ae_loc_init['site_cfg_json'] = json_data.cfg_json || {};
ae_loc_init['site_access_key'] = json_data.access_key || '';
ae_loc_init['site_domain_access_key'] = json_data.site_domain_access_key || '';
ae_loc_init['base_url'] = url.origin;
ae_loc_init['hostname'] = url.hostname;
@@ -183,7 +183,8 @@ export async function load({ fetch, params, parent, route, url }) {
if (access_key == ae_loc_init['site_access_key']) {
ae_loc_init['key_checked'] = ae_loc_init['site_access_key'];
ae_loc_init['allow_access'] = ae_loc_init['site_access_key'];
} else if (access_key == ae_loc_init['site_domain_access_key']) {
}
else if (access_key == ae_loc_init['site_domain_access_key']) {
ae_loc_init['key_checked'] = ae_loc_init['site_domain_access_key'];
ae_loc_init['allow_access'] = ae_loc_init['site_domain_access_key'];
} else {
@@ -195,11 +196,11 @@ export async function load({ fetch, params, parent, route, url }) {
}
}
if (!account_id) {
error(500, {
message: 'The account ID was not found! Check the API.'
});
}
// if (!account_id) {
// error(500, {
// message: 'The account ID was not found! Check the API.'
// });
// }
ae_acct['api'] = ae_api_init;
ae_acct['loc'] = ae_loc_init;
@@ -213,7 +214,9 @@ export async function load({ fetch, params, parent, route, url }) {
sponsorship_cfg_id: ae_loc_init.site_cfg_json?.slct__sponsorship_cfg_id
};
data_struct[ae_loc_init.account_id] = ae_acct;
data_struct[account_id] = ae_acct;
if (log_lvl) console.log('ROOT LOAD: Final data_struct structure ready.', Object.keys(data_struct));
return data_struct;
}