feat: support `cause` for errors in serialization

This commit is contained in:
alexander.akait 2023-05-30 00:40:54 +03:00
parent 5b1fd3ebba
commit e90bd2021f
2 changed files with 48 additions and 0 deletions

View File

@ -21,6 +21,7 @@ class ErrorObjectSerializer {
serialize(obj, context) {
context.write(obj.message);
context.write(obj.stack);
context.write(/** @type {Error & { cause: "unknown" }} */ (obj).cause);
}
/**
* @param {ObjectDeserializerContext} context context
@ -31,6 +32,8 @@ class ErrorObjectSerializer {
err.message = context.read();
err.stack = context.read();
/** @type {Error & { cause: "unknown" }} */
(err).cause = context.read();
return err;
}

View File

@ -41,6 +41,51 @@ describe("Compiler (filesystem caching)", () => {
]
};
options.plugins = [
{
apply(compiler) {
const name = "TestCachePlugin";
compiler.hooks.thisCompilation.tap(name, compilation => {
compilation.hooks.processAssets.tapPromise(
{
name,
stage:
compiler.webpack.Compilation
.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE
},
async () => {
const cache = compilation.getCache(name);
const ident = "test.ext";
const cacheItem = cache.getItemCache(ident, null);
const result = await cacheItem.getPromise(ident);
if (result) {
expect(result.number).toEqual(42);
expect(result.string).toEqual("string");
expect(result.error.cause.message).toEqual("cause");
expect(result.error1.cause.string).toBe("string");
expect(result.error1.cause.number).toBe(42);
return;
}
const number = 42;
const string = "string";
const error = new Error("error", { cause: new Error("cause") });
const error1 = new Error("error", {
cause: { string, number }
});
await cacheItem.storePromise({ number, string, error, error1 });
}
);
});
}
}
];
function runCompiler(onSuccess, onError) {
const c = webpack(options);
c.hooks.compilation.tap(