diff options
author | Vendicated <vendicated@riseup.net> | 2022-10-12 05:35:34 +0200 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2022-10-12 05:35:34 +0200 |
commit | 9b7ebe4680465361542f2cf956bd2f1ef47eca8b (patch) | |
tree | 68f121035aa869cb7fe103ff7dd141042de9830c /src/utils/misc.tsx | |
parent | 8e93c5cb43fc8f375f91a44dc665ddc573435f84 (diff) | |
download | Vencord-9b7ebe4680465361542f2cf956bd2f1ef47eca8b.tar.gz Vencord-9b7ebe4680465361542f2cf956bd2f1ef47eca8b.tar.bz2 Vencord-9b7ebe4680465361542f2cf956bd2f1ef47eca8b.zip |
petpet
Diffstat (limited to 'src/utils/misc.tsx')
-rw-r--r-- | src/utils/misc.tsx | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/utils/misc.tsx b/src/utils/misc.tsx index 7a733ed..dfeb330 100644 --- a/src/utils/misc.tsx +++ b/src/utils/misc.tsx @@ -129,3 +129,31 @@ export function classes(...classes: string[]) { export function sleep(ms: number): Promise<void> { return new Promise(r => setTimeout(r, ms)); } + +/** + * Wraps a Function into a try catch block and logs any errors caught + * Due to the nature of this function, not all paths return a result. + * Thus, for consistency, the returned functions will always return void or Promise<void> + * + * @param name Name identifying the wrapped function. This will appear in the logged errors + * @param func Function (async or sync both work) + * @param thisObject Optional thisObject + * @returns Wrapped Function + */ +export function suppressErrors<F extends Function>(name: string, func: F, thisObject?: any): F { + return (func.constructor.name === "AsyncFunction" + ? async function (this: any) { + try { + await func.apply(thisObject ?? this, arguments); + } catch (e) { + console.error(`Caught an Error in ${name || "anonymous"}\n`, e); + } + } + : function (this: any) { + try { + func.apply(thisObject ?? this, arguments); + } catch (e) { + console.error(`Caught an Error in ${name || "anonymous"}\n`, e); + } + }) as any as F; +} |