@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
78 lines • 3.94 kB
JavaScript
import { doSpHttpFetchOrPostAndCheck } from "../../../components/molecules/SpHttp/Sp/doSpHttpFetch";
import { getCurrentPageLink, getUrlVarsAsStrings } from "../../../logic/Links/UrlFunctions";
import { SecureCDNalyticsWebAbsolute } from "../interfaces/constants";
/**
* 2024-12-09: Migrated this function from it's original location here: \components\molecules\process-results\Logging.ts
*
* import { saveErrorToLog } from "@mikezimm/fps-core-v7/lib/restAPIs/logging/Analytics/saveErrorToLog";
*
* @param errorInfo
* @param errorInput
* @returns
*/
export async function saveErrorToLog(errorInfo, errorInput, fpsSpService) {
//Return if you are not supposed to log an error
if (errorInput.logErrors !== true) {
return;
}
// https://github.com/mikezimm/fps-library-v2/issues/15
if (!errorInfo) {
return;
}
// https://github.com/mikezimm/fps-library-v2/issues/15
if (!fpsSpService) {
console.log(`unable to saveErrorToLog due to no fpsSpService`);
return;
}
const { traceString, alertMe, consoleLog, } = errorInput;
let trace = traceString ? traceString.split('|') : [];
let getParams = JSON.stringify(getUrlVarsAsStrings());
let parsedMessage = errorInfo.returnMess.split('-- FULL ERROR MESSAGE:');
let zzzRichText1 = parsedMessage.length > 0 ? parsedMessage[0] : 'No friendly error';
let zzzRichText2 = parsedMessage.length > 0 ? parsedMessage[1] : errorInfo.returnMess;
let saveItem = {
// https://github.com/fps-solutions/HubCon/issues/103
__metadata: { type: `` },
Title: trace[0],
Result: trace[3] ? trace[3] : null,
zzzText1: trace[4] ? trace[4] : null,
zzzText2: trace[5] ? trace[5] : null,
zzzText3: trace[6] ? trace[6] : null,
zzzText6: typeof alertMe === 'string' ? alertMe : `${alertMe}`,
zzzText7: typeof consoleLog === 'string' ? consoleLog : `${consoleLog}`,
zzzNumber1: trace[7] ? trace[7] : null,
zzzNumber2: trace[8] ? trace[8] : null,
getParams: getParams,
PageLink: getCurrentPageLink(),
zzzRichText1: zzzRichText1,
zzzRichText2: zzzRichText2,
};
let PageLink = saveItem.PageLink;
if (PageLink) {
if (PageLink.Description.length > 254) {
saveItem.Title += ` | PageLink.Desc to long, see zzzText4`;
saveItem.zzzText4 = `${PageLink.Description.split('?')[1]}`;
PageLink.Description = PageLink.Description.split('?')[0];
}
if (PageLink.Url.length > 254) {
const extraText = ` | PageLink.Url to long, see zzzText4`;
saveItem.Title += extraText;
saveItem.zzzText4 = `${PageLink.Url.split('?')[1]}`;
PageLink.Url = `${PageLink.Url.split('?')[0]}/SEE_zzzText4_For_Url_Parameters`;
PageLink.Description += extraText;
}
}
let absoluteWebUrl = trace[1] ? trace[1] : `${SecureCDNalyticsWebAbsolute}`;
let list = trace[2] ? trace[2] : 'GeneralErrorLog';
// https://github.com/fps-solutions/HubCon/issues/103
// Required for non-PnpJS POST operations
saveItem.__metadata = { type: `SP.Data.${list}ListItem` };
// https://github.com/fps-solutions/HubCon/issues/103
// const formDigestValue: string = await getThisFPSDigestValueFromUrl( absoluteWebUrl );
let postAPI = `${absoluteWebUrl}/_api/web/lists/getbytitle('${list}')/items`;
// 2024-12-21: Not sure if this will work passing in '' for headerContentType
doSpHttpFetchOrPostAndCheck(postAPI, 'POST', fpsSpService, 'application/json;odata=nometadata', false, true, 'item', false, saveItem);
// const TEMPAlert = `saveErrorToLog Alert:\n\nerrorInfo\n${JSON.stringify(errorInfo)}\n\nerrorInput:\n\nerrorInput\n${errorInput}`
// alert(TEMPAlert);
}
//# sourceMappingURL=saveErrorToLog.js.map