UNPKG

@launchql/cli

Version:
53 lines (52 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cliExitWithError = void 0; const types_1 = require("@launchql/types"); const logger_1 = require("@launchql/logger"); const pg_cache_1 = require("pg-cache"); const log = new logger_1.Logger('cli'); /** * CLI error utility that logs error information and exits with code 1. * Provides consistent error handling and user experience across all CLI commands. * * IMPORTANT: This function properly cleans up PostgreSQL connections before exiting. */ const cliExitWithError = async (error, context) => { if (error instanceof types_1.LaunchQLError) { // For LaunchQLError instances, use structured logging log.error(`Error: ${error.message}`); // Log additional context if available if (error.context && Object.keys(error.context).length > 0) { log.debug('Error context:', error.context); } // Log any additional context provided if (context) { log.debug('Additional context:', context); } } else if (error instanceof Error) { // For generic Error instances log.error(`Error: ${error.message}`); if (context) { log.debug('Context:', context); } } else if (typeof error === 'string') { // For simple string messages log.error(`Error: ${error}`); if (context) { log.debug('Context:', context); } } // Perform cleanup before exiting try { await (0, pg_cache_1.teardownPgPools)(); log.debug('Database connections cleaned up'); } catch (cleanupError) { log.warn('Failed to cleanup database connections:', cleanupError); // Don't let cleanup errors prevent the exit } process.exit(1); }; exports.cliExitWithError = cliExitWithError;