From 8270a02246fa080626ee51923eb5b7677892417b Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Thu, 7 Apr 2022 23:59:50 +0800 Subject: [PATCH] finally a correct way to throw errors --- package.json | 1 + src/main.ts | 9 ++++++++- src/sync.ts | 12 +++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d95d16a..8273720 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "@fyears/tsqueue": "^1.0.1", "@microsoft/microsoft-graph-client": "^3.0.1", "acorn": "^8.5.0", + "aggregate-error": "^4.0.0", "assert": "^2.0.0", "aws-crt": "^1.10.1", "buffer": "^6.0.3", diff --git a/src/main.ts b/src/main.ts index 7a3ef50..7868fe6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,6 +56,7 @@ import { SyncAlgoV2Modal } from "./syncAlgoV2Notice"; import { applyPresetRulesInplace } from "./presetRules"; import { applyLogWriterInplace, log } from "./moreOnLog"; +import AggregateError from "aggregate-error"; const DEFAULT_SETTINGS: RemotelySavePluginSettings = { s3: DEFAULT_S3_CONFIG, @@ -350,7 +351,13 @@ export default class RemotelySavePlugin extends Plugin { log.error(msg); log.error(error); getNotice(msg, 10 * 1000); - getNotice(error.message, 10 * 1000); + if (error instanceof AggregateError) { + for (const e of error.errors) { + getNotice(e.message, 10 * 1000); + } + } else { + getNotice(error.message, 10 * 1000); + } this.syncStatus = "idle"; if (this.syncRibbon !== undefined) { setIcon(this.syncRibbon, iconNameSyncWait); diff --git a/src/sync.ts b/src/sync.ts index 196a05e..1a643b3 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -5,6 +5,7 @@ import { Vault, requireApiVersion, } from "obsidian"; +import AggregateError from "aggregate-error"; import PQueue from "p-queue"; import { RemoteItem, @@ -1156,6 +1157,7 @@ export const doActualSync = async ( } const queue = new PQueue({ concurrency: concurrency, autoStart: true }); + const potentialErrors: Error[] = []; for (let k = 0; k < singleLevelOps.length; ++k) { const val: FileOrFolderMixedState = singleLevelOps[k]; @@ -1188,10 +1190,18 @@ export const doActualSync = async ( log.debug(`finished ${key}`); }; - queue.add(fn); + + queue.add(fn).catch((e) => { + const msg = `${key}: ${e.message}`; + potentialErrors.push(new Error(msg)); + }); } await queue.onIdle(); + + if (potentialErrors.length > 0) { + throw new AggregateError(potentialErrors); + } } } };