From 60218da0c6eaa08a29dc80b765d9045a6deaa2ab Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Thu, 8 Aug 2024 12:00:55 -0400 Subject: [PATCH] refactor: introduce helper functions for logging Ticket: DX-660 --- packages/openapi-generator/src/cli.ts | 19 +++++++++---------- packages/openapi-generator/src/error.ts | 13 +++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/openapi-generator/src/cli.ts b/packages/openapi-generator/src/cli.ts index 48da7e17..dfd061ef 100644 --- a/packages/openapi-generator/src/cli.ts +++ b/packages/openapi-generator/src/cli.ts @@ -18,6 +18,7 @@ import { KNOWN_IMPORTS } from './knownImports'; import { findSymbolInitializer } from './resolveInit'; import { parseCodecInitializer } from './codec'; import { SourceFile } from './sourceFile'; +import { logError, logInfo, logWarn } from './error'; const app = command({ name: 'api-ts', @@ -87,7 +88,7 @@ const app = command({ const codecFilePath = p.resolve(codecFile); const codecModule = await import(codecFilePath); if (codecModule.default === undefined) { - console.error(`[ERROR] Could not find default export in ${codecFilePath}`); + logError(`Could not find default export in ${codecFilePath}`); process.exit(1); } const customCodecs = codecModule.default(E); @@ -96,13 +97,13 @@ const app = command({ const project = await new Project({}, knownImports).parseEntryPoint(filePath); if (E.isLeft(project)) { - console.error(`[ERROR] ${project.left}`); + logError(`${project.left}`); process.exit(1); } const entryPoint = project.right.get(filePath); if (entryPoint === undefined) { - console.error(`[ERROR] Could not find entry point ${filePath}`); + logError(`Could not find entry point ${filePath}`); process.exit(1); } @@ -119,14 +120,12 @@ const app = command({ symbol.init.callee.type === 'Super' || symbol.init.callee.type === 'Import' ) { - console.error( - `[WARN] Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`, - ); + logWarn(`Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`); continue; } else if (!isApiSpec(entryPoint, symbol.init.callee)) { continue; } - console.error(`[INFO] Found API spec in ${symbol.name}`); + logInfo(`[INFO] Found API spec in ${symbol.name}`); const result = parseApiSpec( project.right, @@ -134,7 +133,7 @@ const app = command({ symbol.init.arguments[0]!.expression, ); if (E.isLeft(result)) { - console.error(`[ERROR] Error when parsing ${symbol.name}: ${result.left}`); + logError(`Error when parsing ${symbol.name}: ${result.left}`); process.exit(1); } @@ -145,7 +144,7 @@ const app = command({ apiSpec.push(...result.right); } if (apiSpec.length === 0) { - console.error(`[ERROR] Could not find API spec in ${filePath}`); + logError(`Could not find API spec in ${filePath}`); process.exit(1); } @@ -166,7 +165,7 @@ const app = command({ } const sourceFile = project.right.get(ref.location); if (sourceFile === undefined) { - console.error(`[ERROR] Could not find '${ref.name}' from '${ref.location}'`); + logError(`Could not find '${ref.name}' from '${ref.location}'`); process.exit(1); } diff --git a/packages/openapi-generator/src/error.ts b/packages/openapi-generator/src/error.ts index e1e1258d..f4bcdb2a 100644 --- a/packages/openapi-generator/src/error.ts +++ b/packages/openapi-generator/src/error.ts @@ -20,3 +20,16 @@ export function errorLeft(message: string): E.Either { export function stripStacktraceOfErrors(errors: string[]) { return errors.map((e) => e!.split('\n')[0]); } + +// helper functions for logging +export function logError(message: string): void { + console.error(`[ERROR] ${message}`); +} + +export function logWarn(message: string): void { + console.error(`[WARN] ${message}`); +} + +export function logInfo(message: string): void { + console.error(`[INFO] ${message}`); +}