[chore] add @Log decorator + add logs for LivShortcuts

This commit is contained in:
MathieuG-P
2024-01-21 15:22:09 +01:00
parent d6aad2690c
commit bb4ce5505a
7 changed files with 108 additions and 6 deletions
+19 -4
View File
@@ -1,7 +1,22 @@
export function tryit<Return>(func: () => Return): {error: Error, result: Return} {
import { isPromise } from "./promise.helpers";
type TryitReturn<Return> = Return extends Promise<any> ? Promise<{ error: Error | null, result: Awaited<Return> | null }> : { error: Error | null, result: Return | null };
export function tryit<Return>(func: () => Return): TryitReturn<Return> {
try {
return { error: null, result: func() };
const result = func();
if(isPromise(result)){
return result
.then((value) => ({ error: null, result: value }))
.catch((err) => ({ error: err instanceof Error ? err : new Error(`${err}`), result: null })) as Return extends Promise<any>
? Promise<{error: Error, result: undefined} | {error: undefined, result: Awaited<Return>}>
: {error: Error, result: undefined} | {error: undefined, result: Return};
}
return { error: undefined, result } as TryitReturn<Return>;
} catch (err) {
return { error: err instanceof Error ? err : new Error(`${err}`), result: null }
return { error: err, result: undefined } as TryitReturn<Return>;
}
}
}
+3
View File
@@ -0,0 +1,3 @@
export function isFunction(value: any): value is Function {
return !!(value && value.constructor && value.call && value.apply)
}
+9
View File
@@ -1,3 +1,5 @@
import { isFunction } from "./function.helpers";
export type AllSettledHelperOptions = {
keepStructure?: boolean;
removeFalsy?: boolean;
@@ -19,3 +21,10 @@ export async function allSettled<T>(promises: Promise<T>[], options?: AllSettled
return acc;
}, []);
}
export function isPromise(value: any): value is Promise<unknown> {
if(!value) { return false; }
if(!value.then) { return false; }
if(!isFunction(value.then)) { return false; }
return true;
}