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

@@ -77,28 +77,32 @@
} }
let ae_acct = data[$slct.account_id]; let ae_acct = data[$slct.account_id];
$ae_api = { if (ae_acct) {
...$ae_api, $ae_api = {
...ae_acct.api ...$ae_api,
}; ...(ae_acct.api || {})
if (log_lvl > 1) { };
console.log(`$ae_api = `, $ae_api); if (log_lvl > 1) {
} console.log(`$ae_api = `, $ae_api);
}
$ae_loc = { $ae_loc = {
...$ae_loc, ...$ae_loc,
...ae_acct.loc ...(ae_acct.loc || {})
}; };
if (log_lvl > 1) { if (log_lvl > 1) {
console.log(`$ae_loc = `, $ae_loc); console.log(`$ae_loc = `, $ae_loc);
} }
$slct = { $slct = {
...$slct, ...$slct,
...ae_acct.slct ...(ae_acct.slct || {})
}; };
if (log_lvl > 1) { if (log_lvl > 1) {
console.log(`$slct = `, $slct); console.log(`$slct = `, $slct);
}
} else {
console.warn('ae_root +layout.svelte: ae_acct not found for account_id:', $slct.account_id);
} }
let flag_clear_idb: boolean = $state(false); let flag_clear_idb: boolean = $state(false);

View File

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

View File

@@ -19,16 +19,16 @@ export async function load({ params, parent }) {
const event_location_id = params.event_location_id; const event_location_id = params.event_location_id;
if (!event_location_id) { if (!event_location_id) {
console.log( console.warn(
`ae events_pres_mgmt location [event_location_id] +page.ts: The event_location_id was not found in the params!!!` `ae events_pres_mgmt location [event_location_id] +page.ts: The event_location_id was not found in the params!!!`
); );
error(404, { // error(404, {
message: 'Location not found' // message: 'Location not found'
}); // });
} }
ae_acct.slct.event_location_id = event_location_id; ae_acct.slct.event_location_id = event_location_id;
if (browser) { if (browser && event_location_id) {
// Load event location object // Load event location object
const load_event_location_obj = await events_func.load_ae_obj_id__event_location({ const load_event_location_obj = await events_func.load_ae_obj_id__event_location({
api_cfg: ae_acct.api, api_cfg: ae_acct.api,

View File

@@ -25,7 +25,7 @@ export async function load({ params, parent, url }) {
// ae_acct.slct.account_id = account_id; // ae_acct.slct.account_id = account_id;
const qry_limit = url.searchParams.get('limit') ?? 19; const qry_limit = parseInt(url.searchParams.get('limit') ?? '19');
if (!qry_limit) { if (!qry_limit) {
console.log(`qry_limit +page.ts: The qry_limit was not found in the params!!!`); console.log(`qry_limit +page.ts: The qry_limit was not found in the params!!!`);
} }