UNPKG

@contentstack/cli-utilities

Version:

Utilities for contentstack projects

53 lines (52 loc) 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleAndLogError = exports.cliErrorHandler = exports.v2Logger = void 0; const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("path")); const logger_1 = tslib_1.__importDefault(require("./logger")); const cliErrorHandler_1 = require("./cliErrorHandler"); const v2Logger = new logger_1.default({ basePath: process.env.CS_CLI_LOG_PATH || path.join(process.cwd(), 'logs') }); exports.v2Logger = v2Logger; const cliErrorHandler = new cliErrorHandler_1.CLIErrorHandler(true); // Enable debug mode for error classification exports.cliErrorHandler = cliErrorHandler; /** * Handles and logs an error by classifying it and logging the relevant details. * * This function uses the `cliErrorHandler` to classify the provided error and logs * the error details using `v2Logger`. If debug information is available, it logs * additional debug details, including a stack trace if not already present. * * @param error - The error to be handled and logged. Can be of any type. * @param context - Optional context information to assist in error classification * and logging. * * @remarks * - The error is always logged with its type, message, and other metadata. * - If debug information is available, it is logged separately with a more specific * debug type and additional details. */ function handleAndLogError(error, context) { const classified = cliErrorHandler.classifyError(error, context); // Always log the error v2Logger.logError({ type: classified.type, message: classified.message, error: classified.error, context: classified.context, hidden: classified.hidden, meta: classified.meta, }); // Log debug information if available if (classified.debug) { v2Logger.logDebug({ type: `${classified.type}_DEBUG`, message: `${classified.message} [DEBUG]`, debug: Object.assign(Object.assign({}, classified.debug), { // Ensure stack trace is included if not already there stackTrace: classified.debug.stackTrace || classified.error.stack }), context: classified.context, meta: classified.meta, }); } } exports.handleAndLogError = handleAndLogError;