Skip to content

Commit

Permalink
♻️ Handle error with better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukáš Horák committed Nov 8, 2022
1 parent 91b665c commit e7c0aef
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
17 changes: 11 additions & 6 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/invariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 6 additions & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
7 changes: 5 additions & 2 deletions packages/core/src/plugins/load.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getErrorMessage } from "../errors";
import type {
NamedLoksePlugin,
PluginFactory,
Expand Down Expand Up @@ -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?`
);
Expand All @@ -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
)}`
);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/plugins/runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { reduce } from "bluebird";
import { getErrorMessage } from "../errors";

import type {
LoksePlugin,
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/reader/spreadsheet-reader.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -98,7 +98,7 @@ export class SpreadsheetReader {
worksheetsLines[title] = lines;
}
} catch (error) {
this.logger.warn(error.message);
this.logger.warn(getErrorMessage(error));
}
});

Expand Down

0 comments on commit e7c0aef

Please sign in to comment.