AggregateErrorSerializer.js 1.5 KB

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