From c53feed2149eab77fe7002360942c61e7332a229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Tue, 28 Mar 2023 16:45:02 +0200 Subject: [PATCH] #789: improve error handling --- lib/metadataTypes/MetadataType.js | 12 +++++++---- lib/metadataTypes/MobileMessage.js | 26 ++++++++++++++++------- lib/metadataTypes/TransactionalMessage.js | 3 +-- lib/metadataTypes/User.js | 5 ++++- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/metadataTypes/MetadataType.js b/lib/metadataTypes/MetadataType.js index 157335183..4948f3f8b 100644 --- a/lib/metadataTypes/MetadataType.js +++ b/lib/metadataTypes/MetadataType.js @@ -677,8 +677,12 @@ class MetadataType { metadataEntry[this.definition.nameField] } / ${metadataEntry[this.definition.nameField]}:` ); - for (const msg of parsedErrors) { - Util.logger.error(' • ' + msg); + if (parsedErrors.length) { + for (const msg of parsedErrors) { + Util.logger.error(' • ' + msg); + } + } else if (ex?.message) { + Util.logger.debug(ex.message); } return null; } @@ -1578,8 +1582,8 @@ class MetadataType { * @returns {string[] | void} formatted Error Message */ static checkForErrors(ex) { + const errors = []; if (ex?.response?.status >= 400 && ex?.response?.status < 600) { - const errors = []; if (ex.response.data.errors) { for (const errMsg of ex.response.data.errors) { errors.push( @@ -1605,8 +1609,8 @@ class MetadataType { } Util.logger.debug(JSON.stringify(ex.config)); Util.logger.debug(JSON.stringify(ex.response.data)); - return errors; } + return errors; } /** diff --git a/lib/metadataTypes/MobileMessage.js b/lib/metadataTypes/MobileMessage.js index 49d347bc8..863212aa6 100644 --- a/lib/metadataTypes/MobileMessage.js +++ b/lib/metadataTypes/MobileMessage.js @@ -343,17 +343,27 @@ class MobileMessage extends MetadataType { */ static async preDeployTasks(metadata, deployDir) { // mobileCode - metadata.code = cache.getByKey('mobileCode', metadata.code.code); + const code = cache.getByKey('mobileCode', metadata.code.code); + if (!code) { + throw new Error(`mobileCode ${metadata.code.code} not found in cache`); + } + metadata.code = code; // mobileKeyword if (metadata.keyword?.keyword) { - metadata.keyword = cache.getByKey('mobilekeyword', metadata.keyword.keyword); + const keyword = cache.getByKey('mobileKeyword', metadata.keyword.keyword); + if (!keyword) { + throw new Error(`mobileKeyword ${metadata.keyword.keyword} not found in cache`); + } + metadata.keyword = keyword; } if (metadata.subscriptionKeyword?.keyword) { - metadata.subscriptionKeyword.keyword = cache.getByKey( - 'mobilekeyword', - metadata.subscriptionKeyword.keyword - ); + const keyword = cache.getByKey('mobileKeyword', metadata.subscriptionKeyword.keyword); + if (!keyword) { + throw new Error(`mobileKeyword ${metadata.keyword.keyword} not found in cache`); + } + + metadata.subscriptionKeyword.keyword = keyword; } // campaign @@ -403,9 +413,9 @@ class MobileMessage extends MetadataType { metadataEntry[this.definition.keyField] = apiResponse.id; Object.assign(apiResponse, result.metadata[apiResponse.id]); // postRetrieveTasks will be run automatically on this via super.saveResult - } catch { + } catch (ex) { throw new Error( - `Could not get details for new ${this.definition.type} ${apiResponse.id} from server` + `Could not get details for new ${this.definition.type} ${apiResponse.id} from server (${ex.message})` ); } } diff --git a/lib/metadataTypes/TransactionalMessage.js b/lib/metadataTypes/TransactionalMessage.js index 283c1fe0f..58587d7db 100644 --- a/lib/metadataTypes/TransactionalMessage.js +++ b/lib/metadataTypes/TransactionalMessage.js @@ -13,8 +13,7 @@ class TransactionalMessage extends MetadataType { // define this.subType as string here for intellisense; requires to be redefined in child class static subType; /** - * Retrieves Metadata of Mobile Keywords - * Endpoint /legacy/v1/beta/mobile/code/ return all Mobile Codes with all details. + * Retrieves Metadata * * @param {string} retrieveDir Directory where retrieved metadata directory will be saved * @param {void} [_] unused parameter diff --git a/lib/metadataTypes/User.js b/lib/metadataTypes/User.js index 168e92583..2e4e8d1e0 100644 --- a/lib/metadataTypes/User.js +++ b/lib/metadataTypes/User.js @@ -142,7 +142,10 @@ class User extends MetadataType { // Locale if (metadata.c__LocaleCode) { // confirm it's a valid locale - cache.getByKey('_language', metadata.c__LocaleCode); + const test = cache.getByKey('_language', metadata.c__LocaleCode); + if (!test) { + throw new Error(`_language ${metadata.c__LocaleCode} not found in cache`); + } metadata.Locale = { LocaleCode: metadata.c__LocaleCode }; delete metadata.c__LocaleCode; }