From 0e2c0b9bfbd51df4a5286da29119979d8283ef4c Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Sun, 18 Aug 2024 01:09:54 -0300 Subject: [PATCH] Cleanup extractAndLoadChunksLazy error handling --- src/utils/lazy.ts | 4 ++-- src/utils/lazyReact.tsx | 2 +- src/webpack/api.tsx | 49 +++++++++++++---------------------------- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts index cd043361f..e1e8f06f1 100644 --- a/src/utils/lazy.ts +++ b/src/utils/lazy.ts @@ -22,7 +22,7 @@ export function makeLazy(factory: () => T, attempts = 5, { isIndirect = false tries++; cache = factory(); if (!cache && attempts === tries && !isIndirect) { - console.error(`makeLazy factory failed:\n\n${factory}`); + console.error(`makeLazy factory failed:\n${factory}`); } } @@ -76,7 +76,7 @@ const handler: ProxyHandler = { export function proxyLazy( factory: () => T, attempts = 5, - errMsg: string | (() => string) = `proxyLazy factory failed:\n\n${factory}`, + errMsg: string | (() => string) = `proxyLazy factory failed:\n${factory}`, primitiveErrMsg = "proxyLazy called on a primitive value.", isChild = false ): T { diff --git a/src/utils/lazyReact.tsx b/src/utils/lazyReact.tsx index 484b1ddcb..640f94edf 100644 --- a/src/utils/lazyReact.tsx +++ b/src/utils/lazyReact.tsx @@ -17,7 +17,7 @@ export const SYM_LAZY_COMPONENT_INNER = Symbol.for("vencord.lazyComponent.inner" * @param attempts How many times to try to get the component before giving up * @returns Result of factory function */ -export function LazyComponent(factory: () => LazyComponentType, attempts = 5, errMsg: string | (() => string) = `LazyComponent factory failed:\n\n${factory}`) { +export function LazyComponent(factory: () => LazyComponentType, attempts = 5, errMsg: string | (() => string) = `LazyComponent factory failed:\n${factory}`) { const get = makeLazy(factory, attempts, { isIndirect: true }); let InnerComponent = null as LazyComponentType | null; diff --git a/src/webpack/api.tsx b/src/webpack/api.tsx index 5251f0523..76ff8368a 100644 --- a/src/webpack/api.tsx +++ b/src/webpack/api.tsx @@ -573,7 +573,7 @@ export function findModuleFactory(...code: CodeFilter) { export function webpackDependantLazy(factory: () => T, attempts?: number) { if (IS_REPORTER) webpackSearchHistory.push(["webpackDependantLazy", [factory]]); - return proxyLazy(factory, attempts, `Webpack dependant lazy factory failed:\n\n${factory}`, "Webpack dependant lazy called on a primitive value. This can happen if you try to destructure a primitive in the top level definition of the lazy."); + return proxyLazy(factory, attempts, `Webpack dependant lazy factory failed:\n${factory}`, "Webpack dependant lazy called on a primitive value. This can happen if you try to destructure a primitive in the top level definition of the lazy."); } /** @@ -588,12 +588,21 @@ export function webpackDependantLazy(factory: () => T, attempts?: numbe export function webpackDependantLazyComponent(factory: () => any, attempts?: number) { if (IS_REPORTER) webpackSearchHistory.push(["webpackDependantLazyComponent", [factory]]); - return LazyComponent(factory, attempts, `Webpack dependant LazyComponent factory failed:\n\n${factory}`); + return LazyComponent(factory, attempts, `Webpack dependant LazyComponent factory failed:\n${factory}`); } export const DefaultExtractAndLoadChunksRegex = /(?:(?:Promise\.all\(\[)?(\i\.e\("?[^)]+?"?\)[^\]]*?)(?:\]\))?|Promise\.resolve\(\))\.then\(\i\.bind\(\i,"?([^)]+?)"?\)\)/; export const ChunkIdsRegex = /\("([^"]+?)"\)/g; +function handleExtractAndLoadChunksError(err: string, code: string | RegExp | CodeFilter, matcher: RegExp) { + if (!IS_DEV || devToolsOpen) { + logger.warn(err, "Code:", code, "Matcher:", matcher); + return false; + } else { + throw new Error(err); + } +} + /** * Extract and load chunks using their entry point. * @@ -606,38 +615,17 @@ export function extractAndLoadChunksLazy(code: string | RegExp | CodeFilter, mat const extractAndLoadChunks = makeLazy(async () => { if (module[SYM_PROXY_INNER_GET] != null && module[SYM_PROXY_INNER_VALUE] == null) { - const err = new Error("extractAndLoadChunks: Couldn't find module factory"); - - if (!IS_DEV || devToolsOpen) { - logger.warn(err, "Code:", code, "Matcher:", matcher); - return false; - } else { - throw err; // Strict behaviour in DevBuilds to fail early and make sure the issue is found - } + return handleExtractAndLoadChunksError("extractAndLoadChunks: Couldn't find module factory", code, matcher); } const match = String(module).match(canonicalizeMatch(matcher)); if (!match) { - const err = new Error("extractAndLoadChunks: Couldn't find chunk loading in module factory code"); - - if (!IS_DEV || devToolsOpen) { - logger.warn(err, "Code:", code, "Matcher:", matcher); - return false; - } else { - throw err; // Strict behaviour in DevBuilds to fail early and make sure the issue is found - } + return handleExtractAndLoadChunksError("extractAndLoadChunks: Couldn't find chunk loading in module factory code", code, matcher); } const [, rawChunkIds, entryPointId] = match; if (Number.isNaN(Number(entryPointId))) { - const err = new Error("extractAndLoadChunks: Matcher didn't return a capturing group with the chunk ids array, or the entry point id returned as the second group wasn't a number"); - - if (!IS_DEV || devToolsOpen) { - logger.warn(err, "Code:", code, "Matcher:", matcher); - return false; - } else { - throw err; // Strict behaviour in DevBuilds to fail early and make sure the issue is found - } + return handleExtractAndLoadChunksError("extractAndLoadChunks: Matcher didn't return a capturing group with the chunk ids array, or the entry point id returned as the second group wasn't a number", code, matcher); } if (rawChunkIds) { @@ -646,14 +634,7 @@ export function extractAndLoadChunksLazy(code: string | RegExp | CodeFilter, mat } if (wreq.m[entryPointId] == null) { - const err = new Error("extractAndLoadChunks: Entry point is not loaded in the module factories, perhaps one of the chunks failed to load"); - - if (!IS_DEV || devToolsOpen) { - logger.warn(err, "Code:", code, "Matcher:", matcher); - return false; - } else { - throw err; // Strict behaviour in DevBuilds to fail early and make sure the issue is found - } + return handleExtractAndLoadChunksError("extractAndLoadChunks: Entry point is not loaded in the module factories, perhaps one of the chunks failed to load", code, matcher); } wreq(Number(entryPointId));