diff --git a/packages/cli/src/commands/update.ts b/packages/cli/src/commands/update.ts index 02fea8cc..7c100ac3 100644 --- a/packages/cli/src/commands/update.ts +++ b/packages/cli/src/commands/update.ts @@ -24,6 +24,7 @@ import Base from "../base"; import { id as idFlag } from "../flags"; import { MissingFlagValue, IncorrectFlagValue } from "../flags/errors"; +import { isCLIError } from "../invariants"; flat.shim(); @@ -256,12 +257,16 @@ class Update extends Base { } catch (error) { spinner.fail(`Generating ${langName} translations failed.`); - const normalizedError = - error instanceof FatalError ? new CLIError(error) : error; - - this.error(normalizedError, { - exit: normalizedError?.oclif?.exit, - }); + if (error instanceof FatalError) { + this.error(new CLIError(error)); + } else if (isCLIError(error)) { + this.error(error, { + // @ts-expect-error For some reason overload doesn't match correct exit type + exit: error.oclif?.exit, + }); + } else if (error instanceof Error || typeof error === "string") { + this.error(error); + } } } } diff --git a/packages/cli/src/invariants.ts b/packages/cli/src/invariants.ts index f7c6dd57..aed4a20f 100644 --- a/packages/cli/src/invariants.ts +++ b/packages/cli/src/invariants.ts @@ -14,3 +14,7 @@ export const noExitCliInvariant = ( expression: unknown, message: string ): void => cliInvariant(expression, message, { exit: false }); + +export function isCLIError(error: unknown): error is CLIError { + return "message" in (error as CLIError); +} diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index 2327447d..4ce601f2 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -37,3 +37,9 @@ export class LangColumnNotFound extends Error { this.lang = lang; } } + +export function getErrorMessage(error: unknown): string { + return "message" in (error as Error) + ? (error as Error).message + : "Unrecognized error"; +} diff --git a/packages/core/src/plugins/load.ts b/packages/core/src/plugins/load.ts index 589aa97a..d7220511 100644 --- a/packages/core/src/plugins/load.ts +++ b/packages/core/src/plugins/load.ts @@ -1,3 +1,4 @@ +import { getErrorMessage } from "../errors"; import type { NamedLoksePlugin, PluginFactory, @@ -31,7 +32,7 @@ function loadPlugin( pluginName, }; } catch (error) { - if (error.code === "MODULE_NOT_FOUND") { + if ((error as { code: string }).code === "MODULE_NOT_FOUND") { options.logger.warn( `🔍 Unable to load plugin ${pluginName}. Is it installed?` ); @@ -41,7 +42,9 @@ function loadPlugin( ); } else { options.logger.warn( - `💥 Unexpected error occurred when loading plugin ${pluginName}:\n${error.message}` + `💥 Unexpected error occurred when loading plugin ${pluginName}:\n${getErrorMessage( + error + )}` ); } diff --git a/packages/core/src/plugins/runner.ts b/packages/core/src/plugins/runner.ts index 940013c5..d639d48b 100644 --- a/packages/core/src/plugins/runner.ts +++ b/packages/core/src/plugins/runner.ts @@ -1,4 +1,5 @@ import { reduce } from "bluebird"; +import { getErrorMessage } from "../errors"; import type { LoksePlugin, @@ -30,8 +31,11 @@ export class PluginsRunner { const transformedTarget = await hook(target, meta); return transformedTarget; } catch (error) { + const { pluginName } = plugin; + const errorMsg = getErrorMessage(error); + this.options.logger.warn( - `Error when running hook ${hookName} of plugin ${plugin.pluginName}: ${error.message}` + `Error when running hook ${hookName} of plugin ${pluginName}: ${errorMsg}` ); return target; } diff --git a/packages/core/src/reader/spreadsheet-reader.ts b/packages/core/src/reader/spreadsheet-reader.ts index cfdb1d2b..a131f131 100644 --- a/packages/core/src/reader/spreadsheet-reader.ts +++ b/packages/core/src/reader/spreadsheet-reader.ts @@ -1,7 +1,7 @@ import { GoogleSpreadsheet } from "google-spreadsheet"; import Line from "../line"; -import { MissingAuthError, FatalError } from "../errors"; +import { MissingAuthError, FatalError, getErrorMessage } from "../errors"; import WorksheetReader from "./worksheet-reader"; import Worksheet from "./worksheet"; import defaultLogger from "../logger"; @@ -61,7 +61,7 @@ export class SpreadsheetReader { try { await this.spreadsheet.loadInfo(); } catch (error) { - throw new FatalError(error.message); + throw new FatalError(getErrorMessage(error)); } this.worksheets = await this.sheetsReader.read(this.spreadsheet); @@ -98,7 +98,7 @@ export class SpreadsheetReader { worksheetsLines[title] = lines; } } catch (error) { - this.logger.warn(error.message); + this.logger.warn(getErrorMessage(error)); } });