From 02b0d0d1a13cb8bcbf7f98339278fd874a0cbc19 Mon Sep 17 00:00:00 2001 From: Corentin THOMASSET Date: Wed, 1 Nov 2023 11:11:51 +0100 Subject: [PATCH 01/89] fix(encryption): alert on decryption error (#711) * update(c-alert): Add variant 'error' * fix(encryption): Alert decryption error (#652) * feat(c-alert): added title * refactor(composable): mutualized computedCatch --------- Co-authored-by: code2933 --- src/composable/computed/catchedComputed.ts | 22 ++++++++++++++++++++++ src/tools/encryption/encryption.vue | 12 +++++++++--- src/ui/c-alert/c-alert.demo.vue | 10 +++++++++- src/ui/c-alert/c-alert.theme.ts | 13 +++++++++++++ src/ui/c-alert/c-alert.vue | 7 +++++-- 5 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 src/composable/computed/catchedComputed.ts diff --git a/src/composable/computed/catchedComputed.ts b/src/composable/computed/catchedComputed.ts new file mode 100644 index 00000000..fd00a128 --- /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 4a348f85..2ad47b51 100644 --- a/src/tools/encryption/encryption.vue +++ b/src/tools/encryption/encryption.vue @@ -1,5 +1,6 @@