Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errorToMessage関数追加し、createLoggerの処理を変更して、eslintのルールを1つ採用 #2212

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment on lines 4 to 9
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここの処理変更によって、console.logだとなんかいい感じに表示される"%o", 25のような書式が使えなくなってしまいます。
たぶん現コードのどこにも使われてないけれども、この変更していいのかが判断しづらい。。
(テンプレート文字列の方が使いやすい気はする)

info: createInner("logInfo"),
Expand Down
10 changes: 0 additions & 10 deletions src/helpers/ensureNotNullish.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイル名が変わりました

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)}`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元のコードだとSymbolが来たときにエラーになってた

}
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
Loading