From 707d688887999b778a93e20fd4e156509056edfb Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Wed, 14 May 2025 16:24:18 -0300 Subject: [PATCH] WebpackPatcher: Try catch more code prone to errors --- src/webpack/patchWebpack.ts | 28 +++++++++++++++++++------- src/webpack/webpack.ts | 40 ++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index 4f5899bc..7d30ee1f 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -60,7 +60,7 @@ export function getFactoryPatchedBy(moduleId: PropertyKey, webpackRequire = wreq return webpackRequire.m[moduleId]?.[SYM_PATCHED_BY]; } -const logger = new Logger("WebpackInterceptor", "#8caaee"); +const logger = new Logger("WebpackPatcher", "#8caaee"); /** Whether we tried to fallback to the WebpackRequire of the factory, or disabled patches */ let wreqFallbackApplied = false; @@ -436,12 +436,21 @@ function runFactoryWithWrap(patchedFactory: PatchedModuleFactory, thisArg: unkno callback(exports, module.id); continue; } + } catch (err) { + logger.error( + "Error while filtering or firing callback for Webpack waitFor subscription:\n", err, + "\n\nModule exports:", exports, + "\n\nFilter:", filter, + "\n\nCallback:", callback + ); + } - if (typeof exports !== "object") { - continue; - } + if (typeof exports !== "object") { + continue; + } - for (const exportKey in exports) { + for (const exportKey in exports) { + try { // Some exports might have not been initialized yet due to circular imports, so try catch it. try { var exportValue = exports[exportKey]; @@ -454,9 +463,14 @@ function runFactoryWithWrap(patchedFactory: PatchedModuleFactory, thisArg: unkno callback(exportValue, module.id); break; } + } catch (err) { + logger.error( + "Error while filtering or firing callback for Webpack waitFor subscription:\n", err, + "\n\nExport value:", exports, + "\n\nFilter:", filter, + "\n\nCallback:", callback + ); } - } catch (err) { - logger.error("Error while firing callback for Webpack waitFor subscription:\n", err, filter, callback); } } diff --git a/src/webpack/webpack.ts b/src/webpack/webpack.ts index c1847474..bce50f3d 100644 --- a/src/webpack/webpack.ts +++ b/src/webpack/webpack.ts @@ -145,9 +145,17 @@ function makePropertyNonEnumerable(target: Record, key: Proper } export function _blacklistBadModules(requireCache: NonNullable, exports: ModuleExports, moduleId: PropertyKey) { - if (shouldIgnoreValue(exports)) { - makePropertyNonEnumerable(requireCache, moduleId); - return true; + try { + if (shouldIgnoreValue(exports)) { + makePropertyNonEnumerable(requireCache, moduleId); + return true; + } + } catch (err) { + logger.error( + "Error while blacklisting module:\n", err, + "\n\nModule id:", moduleId, + "\n\nModule exports:", exports, + ); } if (typeof exports !== "object") { @@ -156,17 +164,25 @@ export function _blacklistBadModules(requireCache: NonNullable