Files
SnowLuma/.pnpm-store/v11/files/cd/3915c196ebc7a94a3405f9cc6d44cea93587b851d05f0255ca00f31dee6db69f6ed2580b49b60b775757ddd318ff5d3e810787aa8ce959d32ed696e6e64445
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

44 lines
1002 B
Plaintext

import { createPlugin } from 'seroval'
import type { PluginInfo, SerovalNode } from 'seroval'
export interface ErrorNode extends PluginInfo {
message: SerovalNode
}
/**
* this plugin serializes only the `message` part of an Error
* this helps with serializing e.g. a ZodError which has functions attached that cannot be serialized
*/
export const ShallowErrorPlugin = /* @__PURE__ */ createPlugin<
Error,
ErrorNode
>({
tag: '$TSR/Error',
test(value) {
return value instanceof Error
},
parse: {
sync(value, ctx) {
return {
message: ctx.parse(value.message),
}
},
async async(value, ctx) {
return {
message: await ctx.parse(value.message),
}
},
stream(value, ctx) {
return {
message: ctx.parse(value.message),
}
},
},
serialize(node, ctx) {
return 'new Error(' + ctx.serialize(node.message) + ')'
},
deserialize(node, ctx) {
return new Error(ctx.deserialize(node.message))
},
})