Errors and sourcemaps inside Virtualenvironment
academo opened this issue · 1 comments
hello.
I am experimenting with near-membrane to isolate plugins executions.
I am using near-membrane-dom to load the plugins code, I create a virtual environment, load the code in a string and use evalute
to get the code executed.
const response = await fetch(pathToPluginJsCode);
const code = await response.text();
const env = createVirtualEnvironment(window, {
// distortions are interceptors to modify the behavior of objects when
// the code inside the sandbox tries to access them
distortionCallback(v) {
// pluginDistortionMap is the distortion definitions, not
return pluginDistortionMap.get(v) ?? v;
},
endowments: Object.getOwnPropertyDescriptors({
// plugins normally run inside system js. I'm using this as mechanism to
// resolve their deps and execute the code
define(deps: string[], jsModule: () => void) => {
const realDeps = resolveDeps(deps);
const pluginExport = code.apply(null, realDeps);
pluginExport.execute(); // the logic is a bit more complex but not relevant for this case.
},
}),
);
- Errors generated inside the virtual environment get printed in the browser console as
Uncaught Proxy(Object) {}
Do you know a good way to unwrap this object to present it human-readable? - I found I can use the instrumentation to partially process these errors myself and I could parse the error stack trace and re-create it and print it nicely (even though the original error is thrown anyway)
- The error itself contains only the error name and message, but the stacktrace, as expected, points you to the near-membrane wrapping code which is not useful to know which actual line of the plugin throw the error.
- Because we evaluate the plugin code as text there's no references to lines of code inside it.
Generally speaking, is there a way to get what line of code generated the error inside the virtual environment? or maybe there's another way to evaluate the plugin's code that doesn't rely on a string so meaningful errors can be generated? There is little to none documentation and I feel force to ask here
Thanks in advance and amazing work in this library.
I kept experimenting with this and the only way I found to make sourcemaps work was to make sure the sourceMappingURL
has either an absolute url or relative to the website hostname. If the sourcemap is (as normally is) relative to the script you are trying to execute it won't load it.