UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

287 lines (285 loc) 16.1 kB
/*** * db .d88b. d888b d888b d888888b d8b db d888b * 88 .8P Y8. 88' Y8b 88' Y8b `88' 888o 88 88' Y8b * 88 88 88 88 88 88 88V8o 88 88 * 88 88 88 88 ooo 88 ooo 88 88 V8o88 88 ooo * 88booo. `8b d8' 88. ~8~ 88. ~8~ .88. 88 V888 88. ~8~ * Y88888P `Y88P' Y888P Y888P Y888888P VP V8P Y888P * import { getHelpfullErrorV2 } from '@mikezimm/npmfunctions/dist/Services/Logging/ErrorHandler'; */ /** * * @param e * @param alertMe * @param consoleLog * @param traceString : Format = webpart|analyticsWeb|analyticsList|result|text1|text2|number1|number2 * @param logErrors : This will log any unknown errors to generic error log */ export function getHelpfullErrorV2(e, alertMe = true, consoleLog = true, traceString, logErrors = true) { let returnMess = convertHelpfullError({ e: e, alertMe: alertMe, consoleLog: consoleLog, traceString: traceString, logErrors: logErrors }); return returnMess; } export function getHelpfullError(e, alertMe = true, consoleLog = true, logErrors = true) { let returnMess = convertHelpfullError({ e: e, alertMe: alertMe, consoleLog: consoleLog, traceString: null, logErrors: logErrors }); return returnMess; } export function convertHelpfullError(inputs) { const { e, alertMe, consoleLog, traceString, } = inputs; if (consoleLog === true) { console.log('convertHelpfullError:', e); } /** * If you change result from 'e', be sure to update in if ( errObj === null... */ let result = 'e'; let errObj = null; if (e.message) { let loc1 = e.message.indexOf("{\""); if (loc1 > 0) { result = e.message.substring(loc1); errObj = JSON.parse(result); } else if (e.message.indexOf('Error making HttpClient request in queryable [404]') > -1) { result = 'Error making HttpClient request in queryable [404]'; } else if (e.message.indexOf('Failed to fetch') > -1) { result = 'Failed to fetch'; } else if (e.message !== null || e.message !== undefined) { result = e.message; } } if (result === 'e') { //Then it didn't find 404 or Failed to Fetch if (errObj === null) { result = 'Unknown error... Sorry :('; } else if (errObj['odata.error']) { result = errObj != null ? errObj['odata.error']['message']['value'] : e.message != null ? e.message : e; } else if (errObj['message']) { result = errObj != null ? errObj['message']['value'] : e.message != null ? e.message : e; } } let friendlyMessage = null; // Error making HttpClient request in queryable [404] ::> if (result.indexOf("A 'PrimitiveValue' node with non-null value was found when trying to read the value of a navigation property") > -1) { friendlyMessage = 'Your Item object may have mis-identied a User column. BE SURE user column is followed by Id such as: EditorId'; } if (result.indexOf('Access denied') > -1) { friendlyMessage = 'Double check your access to this resource.'; } if (result.indexOf('unauthorized') > -1) { friendlyMessage = 'Double check your access to this resource.'; } if (result.indexOf('Failed to fetch') > -1) { friendlyMessage = 'This can happen if the web url is not valid.'; } // Due to organizational policies: Added for https://github.com/mikezimm/SecureScript7/issues/67 if (result.indexOf('Due to organizational policies') > -1 && result.indexOf('download these resources from this device."') > -1) { friendlyMessage = 'Tenant Policy prevents downloads to an unmanaged device.'; } if (result.indexOf('A null value was detected in the items of a collection property value') > -1) { friendlyMessage = 'This can happen if you are saving a null value where an array is expected... Maybe try saving an empty array instead :).'; } if (result.indexOf(`security validation`) > -1 && result.indexOf(`might be corrupted`) > -1) { friendlyMessage = 'Common causes: Invalid or missing DigestValue'; } if (result.indexOf(`An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected.`) > -1) { friendlyMessage = 'Common causes: Saving a Url Object or JSON Object to string column.'; } if (result.indexOf("The formula contains a syntax error or is not supported.") > -1) { friendlyMessage = 'Common causes: You have a formula that is not valid.'; } if (result.indexOf("The formula contains a circular reference") > -1) { friendlyMessage = 'Common causes: The formula refers to the current column which creates circular reference.'; } if (result.indexOf(`An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected`) > -1) { friendlyMessage = 'Common causes: Saving a string to a URL column, saving text to multi-select choice column.'; } if (result.indexOf(`An unexpected 'PrimitiveValue' node was found`) > -1 && result.indexOf(`A 'StartArray' node was expected.`) > -1) { friendlyMessage = 'Common causes: Saving an object or string to an array...\n\naka { UsersId: "results":[ 1,2,3 ] }}'; } if (result.indexOf('does not exist') > -1 && result.indexOf('Column') > -1) { friendlyMessage = 'Missing column: ' + result.split('\'')[1]; } if (result.indexOf('does not exist on type') > -1) { friendlyMessage = 'Missing column: ' + result.split('\'')[1]; } if (result.indexOf('does not exist.') > -1 && result.indexOf('field or property') > -1) { friendlyMessage = 'Missing column: ' + result.split('\'')[1]; } if (result.indexOf('does not exist') > -1 && result.indexOf('List') === 0) { // Found testing vanilla SpHTTP fetch that it already looks ok for a friendly message if (result.indexOf('at site with URL') > 0) { // friendlyMessage = result; } else { friendlyMessage = 'List : ' + result.split('\'')[1] + ' does not exist on this site: ' + result.split('\'')[4]; } } if (result.indexOf('Cannot index this column because') > -1 && result.indexOf('maximum number of columns is currently being indexed') > -1) { friendlyMessage = 'Over Indexing : You already have the maximum number of columns indexed on the list. Not all settings (like choices) will be set properly because of this error.'; } if (result.indexOf('document library with the specified title already exists') > -1) { friendlyMessage = 'List with title already exists.'; } if (result.indexOf('Item does not exist') > -1) { friendlyMessage = 'This can happen if you are trying to find something that well... does not exist:).'; } if (result.indexOf('Cannot find resource for the request SP.UserProfiles') > -1) { friendlyMessage = 'This can happen if you have a typo in a URL:).'; } if (result.indexOf('Invalid JSON. The property name \'\' is not valid.') > -1) { friendlyMessage = 'Check the code for a place where a single quote was replaced by a backtick.'; } if (result.indexOf('Cannot convert a primitive value to the expected type \'Edm.Double\'.') > -1) { friendlyMessage = 'You may be trying to save non-number to Number column :).'; } if (result.indexOf('Cannot convert a primitive value to the expected type \'Edm.String\'.') > -1) { friendlyMessage = 'You may be trying to save a Number or Boolean to String column :).'; } /** * 2025-01-29: Added for https://github.com/mikezimm/drilldown7/issues/472 * since FileSystemObjectType is a valid prop, just NOT for rest filters. */ if (result.indexOf(`FileSystemObjectType' does not exist`) > -1) { friendlyMessage = `If you are using this for a rest filter, try "FSObjType eq X" instead.`; } if (friendlyMessage === null) { /** * https://github.com/mikezimm/drilldown7/issues/409 * Ohh Snap! You may be trying to save a non-DateTime value to a DateTime column :). -- FULL ERROR MESSAGE: * {"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException", * "message":{"lang":"en-US", * "value":"Cannot convert a primitive value to the expected type 'Edm.DateTime'. See the inner exception for more details."}}} */ if (result.indexOf('Cannot convert a primitive value to the expected type \'Edm.') > -1) { let parts = result.split('Cannot convert a primitive value to the expected type \'Edm.'); let expectedType = parts[1]; expectedType = expectedType.substring(0, expectedType.indexOf('\'')); friendlyMessage = `You may be trying to save a non-${expectedType} value to a ${expectedType} column :). If you are trying to clear a date, set it to null instead of "".`; } } if (result.indexOf('One or more column references are not allowed, because the columns are defined as a data type that is not supported in formulas.') > -1) { friendlyMessage = 'You may be trying to use a Hidden or Non-Existant column in a calculated column.'; } if (result.indexOf('The formula refers to a column that does not exist.') > -1) { friendlyMessage = 'Check the formula for spelling mistakes and missing or hidden columns'; } if (result.indexOf('You do not have permission') > -1) { friendlyMessage = 'You do not have access to something. Double check to make sure you are logged in as well!'; } /** * 2025--2025: Added this after I was trying to saveErrorToLog when the PageLink was > 255 characters */ if (result.indexOf('Invalid URL Value') > -1 || result.indexOf('URL field contains invalid') > -1) { friendlyMessage = 'If trying to save a LinkColumn, make sure Url < 255 chars.'; } /** * 2025-01-11: Added this while testing SPApiTester on list that had migrated items with Id in Item Ninja */ if (result.indexOf('0x80131904') > -1) { friendlyMessage = '0x80131904 has been linked to lists where Id was mapped to Id during a migration with Items Nimja web part.'; } /** * 2025-01-11: Added these while testing MERGE requests */ if (result.indexOf('Parsing JSON Light feeds or entries in requests without entity set is not supported') > -1) { friendlyMessage = 'If MERGEing:\nRequires "X-HTTP-Method": "MERGE","IF-MATCH":"*",\n\nMUST NOT have a "Content-Type" header \nOR "Content-Type": "application/json;odata=none".\n'; } if (result.indexOf(`does not match the object's ETag value`) > -1) { friendlyMessage = `May require Header: "IF-MATCH":"*"`; } if (/The parameter .*? does not exist in method GetById/.test(result) === true) { friendlyMessage = 'For a POST, be sure NOT to use getById endpoint.\n\nIf your method is MERGE, you may need Header:\n\n"X-HTTP-Method": "MERGE",\n'; } if (/The type .*? does not support HTTP PATCH method/.test(result) === true) { friendlyMessage = 'If your method is merge, are you MERGING an Item?\n\nIf so, be sure to add the Id in the rest call.\n'; } if (/obj.error = undef/.test(result) === true) { friendlyMessage = 'Use "OData-Version":"4.0" OR NONE.\n\nHad this error when using {`"OData-Version":"3.0"`}\n'; } if (result.indexOf('does not exist on type') > -1 && result.indexOf('ListItem\'') > -1 && result.indexOf('The property') > -1) { if (friendlyMessage != null) { friendlyMessage += ' AND '; } else { friendlyMessage = ''; } friendlyMessage += 'Column: ' + result.split('\'')[1] + ' does not exist on list!'; } if (result.indexOf('does not exist at site with URL') > -1 && result.indexOf('List \'') > -1) { if (friendlyMessage != null) { friendlyMessage += ' AND '; } else { friendlyMessage = ''; } friendlyMessage += 'List or Library: ' + result.split('\'')[1] + ' does not exist on site! Maybe try List Name or List Title instead?'; } // Found in Drilldown when trying to fetch data from a list. if (result.indexOf('query cannot be completed because the number of lookup columns it contains exceeds the lookup column threshold') > -1) { if (friendlyMessage != null) { friendlyMessage += ' AND '; } else { friendlyMessage = ''; } friendlyMessage += 'We can only fetch 10 Lookup type columns (Lookup and People columns). Make sure you only add up to 8 between refiners, views and commands!'; } if (friendlyMessage === null) { if (result.indexOf('Error making HttpClient request in queryable [404]') > -1) { friendlyMessage = 'Check your site or list URL to make sure it is valid. Error [404]'; } else if (result.indexOf('Failed to fetch') > -1) { friendlyMessage = 'Failed to fetch: Check to make sure your Url has the right domain (domain = to the left of /sites/)'; // 2024-12-23: Added this loop based upon testing with Item Ninja https://github.com/fps-solutions/Item-Ninja/issues/62 } else if (result.indexOf('Duplicate Id') > -1 || result.indexOf('already exists.') > -1) { friendlyMessage = 'Duplicate Id -item may already exist!'; } } // Last ditch effort to get the error message: https://github.com/mikezimm/fps-library-v2/issues/33 if (friendlyMessage === null && typeof result === 'string' && result.indexOf(`"value":"`) > 0) { //Then it didn't find 404 or Failed to Fetch if (errObj['odata.error']) { friendlyMessage = errObj != null ? errObj['odata.error']['message']['value'] : e.message != null ? e.message : e; } else if (errObj['message']) { friendlyMessage = errObj != null ? errObj['message']['value'] : e.message != null ? e.message : e; } } let returnMess = friendlyMessage === null ? result : 'Ohh Snap!\n' + friendlyMessage + ' \n-- FULL ERROR MESSAGE: \n' + result; if (consoleLog === true) { console.log('---===>>> ERROR - ErrorHandler Console Log:'); console.log('errObj:', errObj); console.log('e:', e); console.log('result:', result); console.log('returnMess:', returnMess); console.log('traceString:', traceString); } if (alertMe === true) { if (consoleLog !== true) { console.log('---===>>> ERROR - ErrorHandler Console Log:'); console.log('errObj:', errObj); console.log('e:', e); console.log('result:', result); console.log('returnMess:', returnMess); console.log('traceString:', traceString); } if (traceString && traceString.length > 0 && traceString.indexOf('getSiteInfo') > -1) { //Skip this particular case because it breaks Easy Contents when clicking on Site Contents links and Gear Links } else { alert(returnMess); } } /** * https://github.com/fps-solutions/HubCon/issues/117 * 2024-12-30: Changed from null to string so it can more easily be checked in CheckItemsResults to set the final statusText if it where still empty */ if (friendlyMessage === null) friendlyMessage = ''; return { errObj: errObj, friendly: friendlyMessage ? friendlyMessage : '', result: result, returnMess: returnMess, }; } //# sourceMappingURL=friendly.js.map