diff --git a/src/composable/computed/catchedComputed.ts b/src/composable/computed/catchedComputed.ts new file mode 100644 index 000000000..fd00a128f --- /dev/null +++ b/src/composable/computed/catchedComputed.ts @@ -0,0 +1,22 @@ +import { type Ref, ref, watchEffect } from 'vue'; + +export { computedCatch }; + +function computedCatch(getter: () => T, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref, Ref]; +function computedCatch(getter: () => T, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) { + const error = ref(); + const value = ref(); + + watchEffect(() => { + try { + error.value = undefined; + value.value = getter(); + } + catch (err) { + error.value = err instanceof Error ? err.message : err?.toString() ?? defaultErrorMessage; + value.value = defaultValue; + } + }); + + return [value, error] as const; +} diff --git a/src/tools/encryption/encryption.vue b/src/tools/encryption/encryption.vue index 4a348f85b..2ad47b519 100644 --- a/src/tools/encryption/encryption.vue +++ b/src/tools/encryption/encryption.vue @@ -1,5 +1,6 @@