Skip to content

Commit

Permalink
fix: notify user on broken autosaves (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiksta committed Aug 15, 2024
1 parent c4852cc commit 1087d34
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: Release app

on:
workflow_dispatch:
push:
branches:
- main
- dev

jobs:

Expand Down
11 changes: 1 addition & 10 deletions src/renderer/app/debug-info.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -7,15 +7,6 @@ import PackageJson from 'PackageJson';

const LOG = getLogger(__filename);

const trySerialize = (data: any): string | '<Not Serializable>' => {
if (typeof data === 'string') return data;
try {
return JSON.stringify(data, null, 2);
} catch {
return '<Not Serializable>';
}
}

export default function openSystemDebugInfo(openDialog: OpenDialog) {
const displayCopied = new rx.State(false);

Expand Down
23 changes: 20 additions & 3 deletions src/renderer/document/autosave.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(
Expand Down Expand Up @@ -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();
};
}
18 changes: 17 additions & 1 deletion src/renderer/util/DSUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,27 @@ export const DSUtils = {
},

trySerialize: function (data: any): string | '<Not Serializable>' {
if (data instanceof Error) {
return JSON.stringify({
'message': data.message,
'cause': data.cause ?? '<none>',
'error': data.toString(),
'stack': data.stack ?? '<none>',
});
}
if (typeof data === 'string') return data;
try {
return JSON.stringify(data, null, 2);
} catch {
return '<Not Serializable>';
if (typeof data === 'object' && 'toString' in data) {
try {
return data.toString();
} catch {
return '<Not Serializable>';
}
} else {
return '<Not Serializable>';
}
}
},

Expand Down
15 changes: 13 additions & 2 deletions src/renderer/wournal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})

Expand Down

0 comments on commit 1087d34

Please sign in to comment.