refactor(backend_log): improve handle 'file not found'

handle errors more "user friendly" and actually
let the user know, that either the file is not
existing (yet), or that reading the log failed.
This commit is contained in:
Panagiotis Papadopoulos 2025-01-11 13:30:39 +01:00 committed by Panagiotis Papadopoulos
parent 67d858441a
commit dcfdb67539

View File

@ -14,10 +14,16 @@ async function getBackendLog() {
const file = join(LOG_DIR, fileName);
return await readFile(file, "utf8");
} catch (e) {
log.error((e instanceof Error) ? e : `Reading the backend log '${fileName}' failed with an unknown error.`);
const isErrorInstance = e instanceof Error;
// most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977
return "";
if (isErrorInstance && "code" in e && e.code === "ENOENT") {
log.error(e);
return `The backend log file '${fileName}' does not exist (yet).`;
}
log.error(isErrorInstance ? e : `Reading the backend log '${fileName}' failed with an unknown error: '${e}'.`);
return `Reading the backend log '${fileName}' failed.`;
}
}