mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-27 01:56:15 -04:00
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 <code2933@outlook.com>
This commit is contained in:
parent
4d5a67d96d
commit
02b0d0d1a1
5 changed files with 58 additions and 6 deletions
22
src/composable/computed/catchedComputed.ts
Normal file
22
src/composable/computed/catchedComputed.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { type Ref, ref, watchEffect } from 'vue';
|
||||
|
||||
export { computedCatch };
|
||||
|
||||
function computedCatch<T, D>(getter: () => T, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref<T | D>, Ref<string | undefined>];
|
||||
function computedCatch<T, D>(getter: () => T, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) {
|
||||
const error = ref<string | undefined>();
|
||||
const value = ref<T | D | undefined>();
|
||||
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue