From a6ab30c1c10aca5c6e734b031b9e9134000cfba1 Mon Sep 17 00:00:00 2001 From: Gonzalo Avalos Ribas Date: Tue, 30 Jul 2024 17:10:40 -0300 Subject: [PATCH 1/2] Log errors correctly --- src/util/cmdbHierarchyUtils.ts | 5 ++++- src/validateInvocation.ts | 41 ++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/util/cmdbHierarchyUtils.ts b/src/util/cmdbHierarchyUtils.ts index 3894248..0d0a36e 100644 --- a/src/util/cmdbHierarchyUtils.ts +++ b/src/util/cmdbHierarchyUtils.ts @@ -69,7 +69,10 @@ async function getParentClass( return newParent.name; } } catch (error) { - logger.error({ error }, 'Could not find super class'); + logger.error( + { err: error, result: dictionariesPaginatedResponse.result }, + 'Could not find super class', + ); throw error; } } diff --git a/src/validateInvocation.ts b/src/validateInvocation.ts index 1b889cf..0d28f35 100644 --- a/src/validateInvocation.ts +++ b/src/validateInvocation.ts @@ -23,26 +23,33 @@ export default async function validateInvocation( const client = new ServiceNowClient(config, context.logger); await client.validate(); //Validate CMDB configuration. - if (config.cmdb_parent) { - const cmdb_parents = getParsedCMDBClassList(config.cmdb_parent); - if (cmdb_parents.length > 10) { - throw new IntegrationValidationError( - `This integration only supports up to 10 CMDB classes per instance. Please update config to specify 10 or fewer.`, + let response; + try { + if (config.cmdb_parent) { + const cmdb_parents = getParsedCMDBClassList(config.cmdb_parent); + if (cmdb_parents.length > 10) { + throw new IntegrationValidationError( + `This integration only supports up to 10 CMDB classes per instance. Please update config to specify 10 or fewer.`, + ); + } + response = await validateMultipleClasses( + client, + cmdb_parents, + context.logger, ); } - const response = await validateMultipleClasses( - client, - cmdb_parents, - context.logger, + } catch (error) { + throw new IntegrationValidationError( + `Invalid configuration of cmdb. Error: ${error.message}`, ); + } - if (response.invalidClasses.length > 0) { - throw new IntegrationValidationError( - 'CMDB classes are incorrect. ' + - 'The class(es): ' + - response.invalidClasses.join(', ') + - " don't exist in your ServiceNow account. ", - ); - } + if (response.invalidClasses.length > 0) { + throw new IntegrationValidationError( + 'CMDB classes are incorrect. ' + + 'The class(es): ' + + response.invalidClasses.join(', ') + + " don't exist in your ServiceNow account. ", + ); } } From 5395f1d615f2f5d3fc382328a6e47fdcb38339ec Mon Sep 17 00:00:00 2001 From: Gonzalo Avalos Ribas Date: Tue, 30 Jul 2024 17:12:38 -0300 Subject: [PATCH 2/2] correct tests --- src/validateInvocation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/validateInvocation.ts b/src/validateInvocation.ts index 0d28f35..70aaf41 100644 --- a/src/validateInvocation.ts +++ b/src/validateInvocation.ts @@ -44,7 +44,7 @@ export default async function validateInvocation( ); } - if (response.invalidClasses.length > 0) { + if (response?.invalidClasses?.length > 0) { throw new IntegrationValidationError( 'CMDB classes are incorrect. ' + 'The class(es): ' +