player1
player1
TtRPC
Created by player1 on 9/18/2024 in #❓-help
throw custom error
I use @trpc/client as the backbone of a backend. This backend throws custom exceptions based on httpexception. How can I customize exceptions throw in the client?
const client = createTRPCClient<RegistrationAppRouter>({
links: [
loggerLink({
enabled: (options) =>
(config.debug === "error" && options.direction === "down" && options.result instanceof Error) ||
config.debug === "verbose",
}),
httpLink({
url: config.url,
methodOverride: "POST",
transformer: superjson,
}),
],
})
const client = createTRPCClient<RegistrationAppRouter>({
links: [
loggerLink({
enabled: (options) =>
(config.debug === "error" && options.direction === "down" && options.result instanceof Error) ||
config.debug === "verbose",
}),
httpLink({
url: config.url,
methodOverride: "POST",
transformer: superjson,
}),
],
})
I have tried to create a custom link, but observer.error(error) only accepts TrpcError instances
export const customLink: TRPCLink<AppRouter> = () => {
return ({ next, op }) => {
return observable((observer) => {
const unsubscribe = next(op).subscribe({
next(value) {
observer.next(value)
},
error(error) {
observer.error(error)
},
complete() {
observer.complete()
},
})
return unsubscribe
})
}
}
export const customLink: TRPCLink<AppRouter> = () => {
return ({ next, op }) => {
return observable((observer) => {
const unsubscribe = next(op).subscribe({
next(value) {
observer.next(value)
},
error(error) {
observer.error(error)
},
complete() {
observer.complete()
},
})
return unsubscribe
})
}
}
any ideas?
1 replies