2018-10-25 04:23:35 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
|
2018-10-20 00:24:06 +08:00
|
|
|
"use strict";
|
|
|
|
|
2023-05-23 02:32:23 +08:00
|
|
|
/** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
|
|
/** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|
|
|
|
2018-10-20 00:24:06 +08:00
|
|
|
class ErrorObjectSerializer {
|
2023-05-23 02:32:23 +08:00
|
|
|
/**
|
|
|
|
* @param {ErrorConstructor | EvalErrorConstructor | RangeErrorConstructor | ReferenceErrorConstructor | SyntaxErrorConstructor | TypeErrorConstructor} Type error type
|
|
|
|
*/
|
2018-10-25 04:23:35 +08:00
|
|
|
constructor(Type) {
|
|
|
|
this.Type = Type;
|
|
|
|
}
|
2024-07-31 12:23:44 +08:00
|
|
|
|
2023-05-23 02:32:23 +08:00
|
|
|
/**
|
|
|
|
* @param {Error | EvalError | RangeError | ReferenceError | SyntaxError | TypeError} obj error
|
|
|
|
* @param {ObjectSerializerContext} context context
|
|
|
|
*/
|
|
|
|
serialize(obj, context) {
|
|
|
|
context.write(obj.message);
|
|
|
|
context.write(obj.stack);
|
2025-04-23 05:24:52 +08:00
|
|
|
context.write(obj.cause);
|
2018-10-20 00:24:06 +08:00
|
|
|
}
|
2024-07-31 12:23:44 +08:00
|
|
|
|
2023-05-23 02:32:23 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectDeserializerContext} context context
|
|
|
|
* @returns {Error | EvalError | RangeError | ReferenceError | SyntaxError | TypeError} error
|
|
|
|
*/
|
|
|
|
deserialize(context) {
|
2018-10-25 04:23:35 +08:00
|
|
|
const err = new this.Type();
|
2018-10-20 00:24:06 +08:00
|
|
|
|
2023-05-23 02:32:23 +08:00
|
|
|
err.message = context.read();
|
|
|
|
err.stack = context.read();
|
2025-04-23 05:24:52 +08:00
|
|
|
err.cause = context.read();
|
2018-10-20 00:24:06 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ErrorObjectSerializer;
|