| 
									
										
										
										
											2025-04-23 05:24:52 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ | 
					
						
							|  |  |  | /** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 06:27:18 +08:00
										 |  |  | /** @typedef {Error & { cause: unknown, errors: EXPECTED_ANY[] }} AggregateError */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 05:24:52 +08:00
										 |  |  | class AggregateErrorSerializer { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {AggregateError} obj error | 
					
						
							|  |  |  | 	 * @param {ObjectSerializerContext} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	serialize(obj, context) { | 
					
						
							|  |  |  | 		context.write(obj.errors); | 
					
						
							|  |  |  | 		context.write(obj.message); | 
					
						
							|  |  |  | 		context.write(obj.stack); | 
					
						
							|  |  |  | 		context.write(obj.cause); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ObjectDeserializerContext} context context | 
					
						
							|  |  |  | 	 * @returns {AggregateError} error | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	deserialize(context) { | 
					
						
							|  |  |  | 		const errors = context.read(); | 
					
						
							| 
									
										
										
										
											2025-04-23 06:27:18 +08:00
										 |  |  | 		// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
 | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 		// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax, unicorn/error-message
 | 
					
						
							| 
									
										
										
										
											2025-04-23 05:24:52 +08:00
										 |  |  | 		const err = new AggregateError(errors); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		err.message = context.read(); | 
					
						
							|  |  |  | 		err.stack = context.read(); | 
					
						
							|  |  |  | 		err.cause = context.read(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = AggregateErrorSerializer; |