From edd481818939923ebd065f6045c92802de7c17cf Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Mon, 16 Oct 2023 16:31:01 -0400 Subject: [PATCH] Remove cli-lib/errorHandlers from other CLI packages --- .../cli/lib/prompts/projectsLogsPrompt.js | 2 +- packages/serverless-dev-runtime/lib/server.js | 7 +- .../HubSpotAutoUploadPlugin.js | 66 +++++++++++++++---- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/packages/cli/lib/prompts/projectsLogsPrompt.js b/packages/cli/lib/prompts/projectsLogsPrompt.js index a73ec45bc..d81864c13 100644 --- a/packages/cli/lib/prompts/projectsLogsPrompt.js +++ b/packages/cli/lib/prompts/projectsLogsPrompt.js @@ -6,7 +6,7 @@ const { getProjectConfig, ensureProjectExists } = require('../projects'); const { logApiErrorInstance, ApiErrorContext, -} = require('@hubspot/cli-lib/errorHandlers'); +} = require('../../lib/errorHandlers/apiErrors'); const { logger } = require('@hubspot/cli-lib/logger'); const { EXIT_CODES } = require('../enums/exitCodes'); diff --git a/packages/serverless-dev-runtime/lib/server.js b/packages/serverless-dev-runtime/lib/server.js index e2f79c8cc..36c9a429b 100644 --- a/packages/serverless-dev-runtime/lib/server.js +++ b/packages/serverless-dev-runtime/lib/server.js @@ -3,9 +3,6 @@ const bodyParser = require('body-parser'); const cors = require('cors'); const chalk = require('chalk'); const { logger, setLogLevel, LOG_LEVEL } = require('@hubspot/cli-lib/logger'); -const { - logErrorInstance, -} = require('@hubspot/cli-lib/errorHandlers/standardErrors'); const { getTableContents, getTableHeader, @@ -181,7 +178,9 @@ const startServer = async callback => { }, }; } catch (e) { - logErrorInstance(e, { + logger.error(`A system error has occurred: ${e.message}`); + logger.debug(e); + logger.debug({ port, accountId, functionPath, diff --git a/packages/webpack-cms-plugins/HubSpotAutoUploadPlugin.js b/packages/webpack-cms-plugins/HubSpotAutoUploadPlugin.js index ae27ee163..1afdc4a60 100644 --- a/packages/webpack-cms-plugins/HubSpotAutoUploadPlugin.js +++ b/packages/webpack-cms-plugins/HubSpotAutoUploadPlugin.js @@ -5,10 +5,7 @@ const { getAccountId, checkAndWarnGitInclusion, } = require('@hubspot/cli-lib'); -const { - ApiErrorContext, - logApiUploadErrorInstance, -} = require('@hubspot/cli-lib/errorHandlers'); +const { logger } = require('@hubspot/cli-lib/logger'); const { isAllowedExtension } = require('@hubspot/cli-lib/path'); const { LOG_LEVEL, @@ -22,6 +19,41 @@ checkAndWarnGitInclusion(getConfigPath()); const pluginName = 'HubSpotAutoUploadPlugin'; +const parseValidationErrors = (responseBody = {}) => { + const errorMessages = []; + + const { errors, message } = responseBody; + + if (message) { + errorMessages.push(message); + } + + if (errors) { + const specificErrors = errors.map(error => { + let errorMessage = error.message; + if (error.errorTokens && error.errorTokens.line) { + errorMessage = `line ${error.errorTokens.line}: ${errorMessage}`; + } + return errorMessage; + }); + errorMessages.push(...specificErrors); + } + + return errorMessages; +}; + +function logValidationErrors(error, context) { + const { response = {} } = error; + const validationErrors = parseValidationErrors(response.body); + if (validationErrors.length) { + validationErrors.forEach(err => { + logger.error(err); + }); + } + logger.debug(error); + logger.debug(context); +} + class HubSpotAutoUploadPlugin { constructor(options = {}) { const { src, dest, portal, account, autoupload } = options; @@ -67,14 +99,24 @@ class HubSpotAutoUploadPlugin { }) .catch(error => { webpackLogger.error(`Uploading ${dest} failed`); - logApiUploadErrorInstance( - error, - new ApiErrorContext({ - accountId: this.accountId, - request: dest, - payload: filepath, - }) - ); + const context = { + accountId: this.accountId, + request: dest, + payload: filepath, + statusCode: error.statusCode, + }; + if ( + error.statusCode === 400 && + error.response && + error.response.body && + (error.response.body.message || error.response.body.errors) + ) { + logValidationErrors(error, context); + } else { + console.error(error.message); + console.debug(error); + console.debug(context); + } }); }); });