Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
44 lines
1002 B
Plaintext
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))
|
|
},
|
|
})
|