Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

CustomError.ts

31 lines741 Bytes

1import { createSerializationAdapter } from '@tanstack/react-router'
2​
3export class CustomError extends Error {
4 public foo: string
5 public bar: bigint
6​
7 constructor(message: string, options: { foo: string; bar: bigint }) {
8 super(message)
9​
10 Object.setPrototypeOf(this, new.target.prototype)
11​
12 this.name = this.constructor.name
13 this.foo = options.foo
14 this.bar = options.bar
15 }
17​
18export const customErrorAdapter = createSerializationAdapter({
19 key: 'custom-error',
20 test: (v) => v instanceof CustomError,
21 toSerializable: ({ message, foo, bar }) => {
22 return {
23 message,
24 foo,
25 bar,
26 }
27 },
28 fromSerializable: ({ message, foo, bar }) => {
29 return new CustomError(message, { foo, bar })
30 },
31})