@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
78 lines • 4.08 kB
JavaScript
/**
import { check4This } from "@mikezimm/fps-core-v7/lib/logic/Links/CheckSearch";
import { checkDeepProperty } from "@mikezimm/fps-core-v7/lib/logic/Objects/deep";
import { IJSFetchReturn } from "./IJSFetchReturn";
*/
import { check4This, Check4 } from "../../../../logic/Links/CheckSearch";
import { checkDeepProperty } from "../../../../logic/Objects/deep";
export async function addCatchResponseError(results, response, fpsSpService) {
results.ok = response.ok;
results.statusText = response.statusText;
results.statusNo = response.status;
results.status = 'Unknown';
/**
* 2024-10-02: Added this to help provide detailed feedback on error
* https://github.com/fps-solutions/fps-core-v7/issues/6
*/
let errorDetails;
let responseUsed = 'unk'; //
// 2024-12-09: Updated to add a try response.text() in case you get that.
try {
// Attempt to parse the response as JSON
errorDetails = await response.json();
responseUsed = 'json';
}
catch (jsonError) {
// If parsing as JSON fails, log the error and use text instead
console.warn('Failed to parse response as JSON, falling back to text:', jsonError);
try {
// Capture the response body as plain text
errorDetails = await response.text();
responseUsed = 'text';
}
catch (textError) {
console.error('Failed to read response body as text:', textError);
errorDetails = 'Error reading response body';
}
}
const errMessage = checkDeepProperty(errorDetails, ['error', 'message',], 'FullError', true); //errorDetails['odata.error'] && errorDetails['odata.error'].message ? errorDetails['odata.error'].message : 'unknown doSpJsFileFetchOrPost error ~ 93';
// 2024-10-02: Had to force to this format for a known error struture ( when column is missing ) to get it to work in friendly.ms
results.e = { message: typeof errMessage === 'string' ? errMessage : errMessage.value ? errMessage.value : 'Unknown ~addCatchResponse ~42', lang: errMessage.lang ? errMessage.lang : 'Unk', };
/**
* https://github.com/fps-solutions/HubCon/issues/117
* Addedt his to change the status from Unknown when there was an error detected.
*/
if (results.status === 'Unknown')
results.status = 'Error';
if (check4This(Check4.fpsShowFetchResults_Eq_true) === true)
console.log(`fps-core-v7 Api Error: addCatchResponseError ~ 27 results`, results);
if (results.e.message.indexOf('security validation') > -1) {
if (results.errorInfo)
results.errorInfo.friendly = 'Potential missing or invalid digestValue';
console.log('Invalid sfpsSpService: ', fpsSpService);
results.statusText = 'Invalid security validation... fpsSpService?';
results.status = 'Error';
}
return results;
}
export function addUnknownFetchError(results, e) {
results.ok = false;
if (!results.statusNo)
results.statusNo = 667;
/**
* Maybe improve this for when list title is incorrect. Seems to give double error
*/
const mess = results.statusNo === 404 ? 'Page not found. Check your site Url'
: results.statusNo === 403 ? 'You may not have Permissions'
: e.message ? e.message : 'Unknown Error'; // Added per Co-Pilot for better error handling
if (!results.e)
results.e = { message: mess, lang: 'Unknown', };
if (!results.statusText)
results.statusText = results.statusNo === 403 ? 'InvalidSecurity - security validation... digestValue?' : `Error TryCatch`;
if (results.statusNo === 403 && (!results.status || results.status === 'Error'))
results.status = 'InvalidSecurity';
if (check4This(Check4.fpsShowFetchResults_Eq_true) === true)
console.log(`fps-core-v7 Logic Error: setUnknownFetchErrorResult ~ 58 fetchAPI`, results.fetchAPI);
return results;
}
//# sourceMappingURL=SpFetchCommon.js.map