| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | /** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 8 | /** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {Error & { cause?: unknown, errors: EXPECTED_ANY[] }} AggregateError */
|
|---|
| 11 |
|
|---|
| 12 | class AggregateErrorSerializer {
|
|---|
| 13 | /**
|
|---|
| 14 | * Serializes this instance into the provided serializer context.
|
|---|
| 15 | * @param {AggregateError} obj error
|
|---|
| 16 | * @param {ObjectSerializerContext} context context
|
|---|
| 17 | */
|
|---|
| 18 | serialize(obj, context) {
|
|---|
| 19 | context.write(obj.errors);
|
|---|
| 20 | context.write(obj.message);
|
|---|
| 21 | context.write(obj.stack);
|
|---|
| 22 | context.write(obj.cause);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Restores this instance from the provided deserializer context.
|
|---|
| 27 | * @param {ObjectDeserializerContext} context context
|
|---|
| 28 | * @returns {AggregateError} error
|
|---|
| 29 | */
|
|---|
| 30 | deserialize(context) {
|
|---|
| 31 | const errors = context.read();
|
|---|
| 32 | // eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax, unicorn/error-message
|
|---|
| 33 | const err = new AggregateError(errors);
|
|---|
| 34 |
|
|---|
| 35 | err.message = context.read();
|
|---|
| 36 | err.stack = context.read();
|
|---|
| 37 | err.cause = context.read();
|
|---|
| 38 |
|
|---|
| 39 | return err;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | module.exports = AggregateErrorSerializer;
|
|---|