Better error handling if the site domain is not found or returned.

This commit is contained in:
Scott Idem
2025-01-28 18:47:30 -05:00
parent acff856e25
commit bdea373c7f
2 changed files with 24 additions and 137 deletions

View File

@@ -16,7 +16,6 @@ export function add_url_params(
}
) {
if (log_lvl) {
// console.log(`*** add_url_params() ***`);
console.log(`*** add_url_params() *** base_url=${base_url} endpoint=${endpoint}`, params);
}
@@ -45,7 +44,7 @@ export function clean_headers(
}
) {
if (log_lvl) {
console.log(`*** clean_headers() ***`);
console.log(`*** clean_headers() ***`, headers);
}
let headers_cleaned: key_val = {};

View File

@@ -57,7 +57,7 @@ let ae_loc_init: key_val = {
// properties: params, route, url
// functions: fetch, setHeaders, parent, depends, untrack
export async function load({ fetch, params, parent, route, url }) { // params, route, url
let log_lvl: number = 2;
let log_lvl: number = 0;
// console.log(`Svelte root +layout.ts params:`, params);
// console.log(`Svelte root +layout.ts route:`, route);
// console.log(`Svelte root +layout.ts url:`, url);
@@ -141,10 +141,6 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
'use_alt_base': true,
};
// const api_url = new URL(api_endpoint, ae_api_init['base_url']);
// Object.keys(api_params).forEach(key => api_url.searchParams.append(key, api_params[key]));
// let api_url_str = api_url.toString();
const api_url_str = core_func.add_url_params({
base_url: ae_api_init['base_url'],
endpoint: api_endpoint,
@@ -161,23 +157,6 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
log_lvl: log_lvl
});
// Clean the headers
// let headers_cleaned: key_val = {};
// for (const prop in headers) {
// let prop_cleaned = prop.replaceAll('_', '-');
// if (typeof headers[prop] != 'string') {
// headers[prop] = JSON.stringify(headers[prop]);
// }
// headers_cleaned[prop_cleaned] = headers[prop];
// if (log_lvl > 1) {
// console.log(`${prop_cleaned}: ${headers_cleaned[prop_cleaned]}`);
// }
// }
// headers = headers_cleaned;
// if (log_lvl > 1) {
// console.log('All headers cleaned:', headers);
// }
const fetchOptions: RequestInit = {
method: 'GET',
headers: {
@@ -191,7 +170,7 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
.then(async function (response) {
if (response.ok) {
let json_raw = await response.json();
console.log(`ALERT json_raw = `, json_raw);
// console.log(`ALERT json_raw = `, json_raw);
let json_data: any = null;
@@ -252,28 +231,38 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
}
}
} else {
console.log('API GET Object *fetch* response was missing the JSON "data" part.');
error(500, {
message: 'Site lookup failed!'
});
console.log('API GET Object *fetch* response was missing the JSON "data" part. This should not happen.');
return false;
}
return json_data; // || json_raw;
} else if (response.status === 404) {
console.log('API GET Object *fetch* response was a 404 not found.');
return null;
} else {
console.log('API GET Object *fetch* request was no ok.');
error(500, {
message: 'Site lookup failed!'
});
console.log('API GET Object *fetch* request was not ok for some other reason.');
return false;
}
})
.catch(function (error) {
console.log('API GET Object *fetch* request was aborted or failed in an unexpected way.', error);
error(500, {
message: 'Site lookup failed!'
message: 'Site lookup aborted or failed! Check the network connection, domain name, and API.'
});
});
console.log(`ALERT response = `, result);
if (result === null) {
error(403, {
message: 'The site lookup failed! Check that the domain name is configured and enabled.'
});
} else if (result === false) {
error(500, {
message: 'Site lookup failed or returned an unexpected response! Check the API.'
});
}
if (log_lvl > 1) {
console.log(`root +layout.ts: Using account_id = `, account_id);
console.log(`root +layout.ts: ae_loc_init = `, ae_loc_init);
@@ -281,113 +270,12 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
log_lvl = 1;
// if (response.ok) {
// let json_val = await response.json();
// console.log(`ALERT json_val = `, json_val);
// let json_data = json_val.data;
// }
// error(500, {
// message: 'Site lookup failed!'
// });
// account_id = await core_func.handle_load_ae_obj_id__site_domain({
// api_cfg: ae_api_init,
// // fqdn: 'idaa.localhost:5173', // url.host,
// fqdn: url.host,
// try_cache: false,
// log_lvl: log_lvl
// })
// .then(function (site_domain_results) {
// if (site_domain_results) {
// // console.log(`ae_ site_domain_results = `, site_domain_results);
// data_struct.account_id = site_domain_results.account_id_random;
// ae_acct.account_id = site_domain_results.account_id_random;
// ae_api_init['account_id'] = site_domain_results.account_id_random;
// ae_api_init['headers']['x-account-id'] = site_domain_results.account_id_random;
// ae_api_init['headers']['x-no-account-id'] = null;
// ae_loc_init['account_id'] = site_domain_results.account_id_random;
// ae_loc_init['account_code'] = site_domain_results.account_code; // Useful for export file naming
// ae_loc_init['account_name'] = site_domain_results.account_name; // Generally useful for display
// ae_loc_init['site_id'] = site_domain_results.site_id_random;
// ae_loc_init['site_domain_id'] = site_domain_results.site_domain_id_random;
// ae_loc_init['site_enable'] = site_domain_results.enable;
// ae_loc_init['site_header_image_path'] = site_domain_results.header_image_path;
// ae_loc_init['site_style_href'] = site_domain_results.style_href;
// ae_loc_init['site_google_tracking_id'] = site_domain_results.google_tracking_id;
// ae_loc_init['site_access_code_kv'] = site_domain_results.access_code_kv_json;
// ae_loc_init['site_cfg_json'] = site_domain_results.cfg_json;
// ae_loc_init['site_access_key'] = site_domain_results.access_key; // This is the general site access key
// ae_loc_init['site_domain_access_key'] = site_domain_results.site_domain_access_key; // This is specific to a (sub)domain.
// if (!ae_loc_init['site_access_key'] && !ae_loc_init['site_domain_access_key']) {
// ae_loc_init['key_checked'] = true; // Se to true to allow access without a key.
// ae_loc_init['allow_access'] = true; // No access key is required here.
// } else {
// let access_key = url.searchParams.get('key');
// if (access_key) {
// if (log_lvl) {
// console.log(`root +layout.ts: access_key = ${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['allow_access'] = ae_loc_init['site_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 {
// // The key changed and no longer matches the site or domain key.
// ae_loc_init['key_checked'] = true;
// ae_loc_init['allow_access'] = false;
// }
// } else {
// if (log_lvl > 1) {
// console.log(`root +layout.ts: No access key found in URL.`);
// }
// ae_loc_init['key_checked'] = true;
// // Do nothing to change the allow_access here
// // ae_loc_init['allow_access'] = false; // An access key is required at this point.
// }
// }
// if (log_lvl > 1) {
// console.log(`root +layout.ts: Returning account_id = `, site_domain_results.account_id_random);
// console.log(`root +layout.ts: ae_loc_init = `, ae_loc_init);
// }
// return site_domain_results.account_id_random;
// }
// console.log(`root +layout.ts: Site domain results not found!!! URL = `, url);
// return null; // For no results
// })
// .catch(function (error) {
// console.log(`root +layout.ts: Site domain lookup error = `, error);
// // error(500, {
// // message: 'Site lookup failed!'
// // });
// return false; // For a likely API or DB connection error
// });
if (await account_id === null) {
console.log(`root +layout.ts: The account_id was not found in API response!!!`);
if (!account_id) {
console.log(`root +layout.ts: The account_id was not found!!!`);
error(500, {
message: 'The account ID was not found!'
});
} else if (await account_id === false) {
console.log(`root +layout.ts: The API site lookup may have failed!!!`);
error(500, {
message: 'Site lookup failed! Check the API.'
});
}
ae_acct['api'] = ae_api_init;