AggregateErrorSerializer.js 1.2 KB

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