Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rethrow cached module errors without wrapping
Summary: Changelog: * **[Fix]:** `metro-runtime`: Rethrow cached module errors without wrapping Metro's `require()` and `import` implementation caches errors thrown during module evaluation, and doesn't re-evaluate modules (except for HMR). Here, we keep this behaviour, but will now always throw the original (cached) error object instead of wrapping it in a new one. This is so that we can show information about the original error in UIs like LogBox. ## Side note: Why cache errors? Caching errors (without wrapping) is what Webpack and Node have aligned on for ES modules, though FWIW their legacy `require` runtimes behave quite differently: * Node re-evaluates throwing modules on every `require`, so assuming a module that throws deterministically, the error object is new every time. (This is what I was going to propose before doing the rest of this research.) * In Webpack only the first `require` throws and subsequent calls fail silently) Given this alignment in the ESM space, the fact that Metro mixes ESM and CommonJS in a single module registry, and how long the caching behaviour has existed in Metro - it's best *not* to add re-evaluation semantics to Metro's `require()` at this point. ## Should we use [`Error.prototype.cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) ? We could in theory add the cached error as the `cause` of the new error, which would allow error handlers to keep track of both the "outer" and "inner" stack traces. However, this would require more nontrivial changes (particularly to React Native's LogBox and ExceptionsManager) to be truly useful, so I'm not doing it here. Reviewed By: huntie Differential Revision: D41475884 fbshipit-source-id: 0a1da0118a0ade29d55803dee5e7d00f6d3ad728
- Loading branch information