From 1087d349395a508fd3d80a9054aa209b861d45ea Mon Sep 17 00:00:00 2001 From: dominiksta Date: Thu, 15 Aug 2024 19:40:52 +0200 Subject: [PATCH] fix: notify user on broken autosaves (#15) --- .github/workflows/release.yaml | 4 ++++ src/renderer/app/debug-info.ts | 11 +---------- src/renderer/document/autosave.ts | 23 ++++++++++++++++++++--- src/renderer/util/DSUtils.ts | 18 +++++++++++++++++- src/renderer/wournal.ts | 15 +++++++++++++-- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index ae3da1a..948e973 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -2,6 +2,10 @@ name: Release app on: workflow_dispatch: + push: + branches: + - main + - dev jobs: diff --git a/src/renderer/app/debug-info.ts b/src/renderer/app/debug-info.ts index 232bd1e..c1a0d00 100644 --- a/src/renderer/app/debug-info.ts +++ b/src/renderer/app/debug-info.ts @@ -1,4 +1,4 @@ -import { Component, h, rx } from "@mvuijs/core"; +import { h, rx } from "@mvuijs/core"; import { OpenDialog } from "common/dialog-manager"; import * as ui5 from '@mvuijs/ui5'; import { getLogger, getLogHistoryText } from "util/Logging"; @@ -7,15 +7,6 @@ import PackageJson from 'PackageJson'; const LOG = getLogger(__filename); -const trySerialize = (data: any): string | '' => { - if (typeof data === 'string') return data; - try { - return JSON.stringify(data, null, 2); - } catch { - return ''; - } -} - export default function openSystemDebugInfo(openDialog: OpenDialog) { const displayCopied = new rx.State(false); diff --git a/src/renderer/document/autosave.ts b/src/renderer/document/autosave.ts index 3268005..2ddb529 100644 --- a/src/renderer/document/autosave.ts +++ b/src/renderer/document/autosave.ts @@ -1,5 +1,6 @@ import { inject } from "dependency-injection"; import { AutosaveConfig } from "persistence/ConfigDTO"; +import { DSUtils } from "util/DSUtils"; import { FileUtils } from "util/FileUtils"; import { getLogger } from "util/Logging"; import { WournalDocument } from "./WournalDocument"; @@ -13,6 +14,7 @@ export const AUTOSAVE_DIR = navigator.userAgent.indexOf('Windows') !== -1 export default function setupAutosave( cfg: AutosaveConfig, currentDoc: () => WournalDocument, + notify: (msg: string) => void, ): () => void { const fs = inject('FileSystem'); LOG.info( @@ -82,19 +84,34 @@ export default function setupAutosave( } } } catch(e) { - LOG.warn(`Could not delete autosaves`, e); + const msg = 'Could not delete autosaves'; + notify(`${msg}. Error: ${DSUtils.trySerialize(e)}`); + LOG.warn(msg, e); + stop(); } const now = new Date(); now.getFullYear() const fileName = autosaveFileName(doc); LOG.info(`Saving autosave ${fileName}`); - fs.write(AUTOSAVE_DIR + fileName, await doc.toFile()); + try { + fs.write(AUTOSAVE_DIR + fileName, await doc.toFile()); + } catch (e) { + const msg = 'Could not write autosave'; + notify(`${msg}. Error: ${DSUtils.trySerialize(e)}`); + LOG.warn(msg, e); + stop(); + } }, cfg.intervalSeconds * 1000) as any as number; + function stop() { + clearInterval(interval); + LOG.info('Autosaves Stopped'); + } + return () => { LOG.warn('Disabling autosave'); - clearInterval(interval); + stop(); }; } diff --git a/src/renderer/util/DSUtils.ts b/src/renderer/util/DSUtils.ts index 89dc0cf..91348ba 100644 --- a/src/renderer/util/DSUtils.ts +++ b/src/renderer/util/DSUtils.ts @@ -38,11 +38,27 @@ export const DSUtils = { }, trySerialize: function (data: any): string | '' { + if (data instanceof Error) { + return JSON.stringify({ + 'message': data.message, + 'cause': data.cause ?? '', + 'error': data.toString(), + 'stack': data.stack ?? '', + }); + } if (typeof data === 'string') return data; try { return JSON.stringify(data, null, 2); } catch { - return ''; + if (typeof data === 'object' && 'toString' in data) { + try { + return data.toString(); + } catch { + return ''; + } + } else { + return ''; + } } }, diff --git a/src/renderer/wournal.ts b/src/renderer/wournal.ts index 99a6424..2cf2013 100644 --- a/src/renderer/wournal.ts +++ b/src/renderer/wournal.ts @@ -513,8 +513,19 @@ export default class Wournal extends Component { this.subscribe(this.configCtx, v => this.confRepo.save(v)); this.onRendered(() => { - const stopAutoSave = - setupAutosave(this.configCtx.value.autosave, () => this.doc.value); + const stopAutoSave = setupAutosave( + this.configCtx.value.autosave, () => this.doc.value, + msg => this.dialog.infoBox('Autosave Error', [ + h.p([ + 'Something went wrong with the autosave system.', + 'Please check file permissions on the autosave directory', + '(%USERPROFILE%\AppData\Roaming\Wournal\autosave on Windows, ', + '~/.cache/wournal/autosave/ on linux). If the error persists, ', + 'you can disable the autosave system in the settings panel.', + ]), + h.p(['The error message was: ', msg]), + ], 'Warning'), + ); this.onRemoved(stopAutoSave); })