Skip to content

Commit

Permalink
errorToMessage作ってeslintの例外ルールをなくす
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Aug 11, 2024
1 parent 713ae91 commit a2b10ad
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 29 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ const tsEslintOptions = {
};

const tsEslintRules = {
// Template String Literalでcatchなどからのunknownを入れられなくなるので無効化
// TODO: いずれは有効化する
"@typescript-eslint/restrict-template-expressions": "off",
// Storeでよくasyncなしの関数を定義するので無効化
// TODO: いずれは有効化する
"@typescript-eslint/require-await": "off",
Expand Down
2 changes: 1 addition & 1 deletion src/backend/browser/browserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defaultEngine, directoryHandleStoreKey } from "./contract";

import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager";
import { ConfigType, EngineId, engineSettingSchema } from "@/type/preload";
import { ensureNotNullish } from "@/helpers/ensureNotNullish";
import { ensureNotNullish } from "@/helpers/errorHelper";
import { UnreachableError } from "@/type/utility";

const dbName = `${import.meta.env.VITE_APP_NAME}-web`;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/browser/fileImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export const showOpenFilePickerImpl = async (options: {
}
return handles.length > 0 ? paths : undefined;
} catch (e) {
log.warn(`showOpenFilePicker error: ${e}`);
log.warn("showOpenFilePicker error:", e);
return undefined;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/backend/common/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
PresetKey,
envEngineInfoSchema,
} from "@/type/preload";
import { ensureNotNullish } from "@/helpers/ensureNotNullish";
import { ensureNotNullish } from "@/helpers/errorHelper";

const lockKey = "save";

Expand Down
6 changes: 3 additions & 3 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ if (!fs.existsSync(vvppEngineDir)) {

const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => {
const engineId = engineInfo.uuid;
log.error(`ENGINE ${engineId} ERROR: ${error}`);
log.error(`ENGINE ${engineId} ERROR:`, error);

// winが作られる前にエラーが発生した場合はwinへの通知を諦める
// FIXME: winが作られた後にエンジンを起動させる
Expand Down Expand Up @@ -213,7 +213,7 @@ async function installVvppEngine(vvppPath: string) {
"インストールエラー",
`${vvppPath} をインストールできませんでした。`,
);
log.error(`Failed to install ${vvppPath}, ${e}`);
log.error(`Failed to install ${vvppPath},`, e);
return false;
}
}
Expand Down Expand Up @@ -308,7 +308,7 @@ async function uninstallVvppEngine(engineId: EngineId) {
"アンインストールエラー",
`${engineName} をアンインストールできませんでした。`,
);
log.error(`Failed to uninstall ${engineId}, ${e}`);
log.error(`Failed to uninstall ${engineId},`, e);
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/electron/manager/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class RuntimeInfoManager {
} catch (e) {
// ディスクの空き容量がない、他ツールからのファイルロック時をトラップ。
// サードパーティ向けなのでVOICEVOX側には通知せず、エラー記録して継続
log.error(`Failed to write file : ${e}`);
log.error("Failed to write file :", e);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/electron/manager/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export class EngineManager {
let errorNotified = false;

engineProcess.on("error", (err) => {
log.error(`ENGINE ${engineId} ERROR: ${err}`);
log.error(`ENGINE ${engineId} ERROR:`, err);
if (!errorNotified) {
errorNotified = true;
this.onEngineProcessError(engineInfo, err);
Expand Down
9 changes: 5 additions & 4 deletions src/backend/electron/manager/vvppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
minimumEngineManifestSchema,
MinimumEngineManifestType,
} from "@/type/preload";
import { errorToMessage } from "@/helpers/errorHelper";

const isNotWin = process.platform !== "win32";

Expand Down Expand Up @@ -320,10 +321,10 @@ export class VvppManager {
log.error(e);
dialog.showErrorBox(
"エンジン削除エラー",
`エンジンの削除に失敗しました。エンジンのフォルダを手動で削除してください。\n${deletingEngineDir}\nエラー内容: ${e}`,
`エンジンの削除に失敗しました。エンジンのフォルダを手動で削除してください。\n${deletingEngineDir}\nエラー内容: ${errorToMessage(e)}`,
);
} else {
log.error(`Failed to rename engine directory: ${e}, retrying`);
log.error("Failed to rename engine directory: ", e, ", retrying");
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
Expand All @@ -344,10 +345,10 @@ export class VvppManager {
log.error(e);
dialog.showErrorBox(
"エンジン追加エラー",
`エンジンの追加に失敗しました。エンジンのフォルダを手動で移動してください。\n${from}\nエラー内容: ${e}`,
`エンジンの追加に失敗しました。エンジンのフォルダを手動で移動してください。\n${from}\nエラー内容: ${errorToMessage(e)}`,
);
} else {
log.error(`Failed to rename engine directory: ${e}, retrying`);
log.error("Failed to rename engine directory: ", e, ", retrying");
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/domain/frontend/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function createLogger(scope: string) {
const createInner =
(method: "logInfo" | "logError" | "logWarn") =>
(...args: unknown[]) => {
window.backend[method](`[${scope}] ${args[0]}`, ...args.slice(1));
window.backend[method](`[${scope}]`, ...args);
};
return {
info: createInner("logInfo"),
Expand Down
10 changes: 0 additions & 10 deletions src/helpers/ensureNotNullish.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/helpers/errorHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** 入力がnullかundefinedの場合エラーを投げ、それ以外の場合は入力をそのまま返す */
export const ensureNotNullish = <T>(
value: T | null | undefined,
message = "Unexpected nullish value",
): T => {
if (value == null) {
throw new Error(message);
}
return value;
};

/** エラーからエラー文を作る */
export const errorToMessage = (e: unknown): string => {
if (e instanceof Error) {
return `${e.toString()}: ${e.message}`;
} else if (typeof e === "string") {
return `String Error: ${e}`;
} else if (typeof e === "object" && e != undefined) {
return `Object Error: ${JSON.stringify(e).slice(0, 100)}`;
} else {
return `Unknown Error: ${String(e)}`;
}
};
4 changes: 2 additions & 2 deletions src/helpers/mapHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/** Mapから値を取得する。指定したキーが存在しない場合は例外を投げる */
export const getOrThrow = <K, V>(map: Map<K, V>, key: K) => {
if (!map.has(key)) {
throw new Error(`Key not found: ${key}`);
throw new Error(`Key not found: ${String(key)}`);
}
return map.get(key) as V;
};

/** Mapから値を削除する。指定したキーが存在しない場合は例外を投げる */
export const deleteOrThrow = <K, V>(map: Map<K, V>, key: K) => {
if (!map.has(key)) {
throw new Error(`Key not found: ${key}`);
throw new Error(`Key not found: ${String(key)}`);
}
map.delete(key);
};
5 changes: 4 additions & 1 deletion src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { generateWriteErrorMessage } from "@/helpers/fileHelper";
import { uuid4 } from "@/helpers/random";
import { cloneWithUnwrapProxy } from "@/helpers/cloneWithUnwrapProxy";
import { UnreachableError } from "@/type/utility";
import { errorToMessage } from "@/helpers/errorHelper";

function generateAudioKey() {
return AudioKey(uuid4());
Expand Down Expand Up @@ -2138,7 +2139,9 @@ export const audioCommandStore = transformCommandStore(
if (Object.keys(errors).length > 0) {
throw new Error(
`話者の変更に失敗しました:\n${Object.entries(errors)
.map(([audioKey, error]) => `${audioKey}${error}`)
.map(
([audioKey, error]) => `${audioKey}${errorToMessage(error)}`,
)
.join("\n")}`,
);
}
Expand Down

0 comments on commit a2b10ad

Please sign in to comment.