feat: improve structure and error handling
This commit is contained in:
+1
-1
@@ -2,4 +2,4 @@ import { createTRPCReact } from "@trpc/react-query"
|
||||
|
||||
import type { AppRouter } from "@/server"
|
||||
|
||||
export const trpc = createTRPCReact<AppRouter>({})
|
||||
export const trpc = createTRPCReact<AppRouter>()
|
||||
|
||||
+25
-1
@@ -1,9 +1,33 @@
|
||||
import { TRPCError } from "@trpc/server"
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { appRouter } from "@/server"
|
||||
import { createContext } from "@/server/context"
|
||||
import { internalServerError } from "@/server/errors/next"
|
||||
import { createCallerFactory } from "@/server/trpc"
|
||||
|
||||
const createCaller = createCallerFactory(appRouter)
|
||||
|
||||
export function serverClient() {
|
||||
return createCaller(createContext())
|
||||
return createCaller(createContext(), {
|
||||
onError: ({ ctx, error, input, path, type }) => {
|
||||
console.error(`Server Client error for ${type}: ${path}`)
|
||||
if (input) {
|
||||
console.error(`Received input:`)
|
||||
console.error(input)
|
||||
}
|
||||
console.error(error)
|
||||
|
||||
if (error instanceof TRPCError) {
|
||||
if (error.code === "UNAUTHORIZED") {
|
||||
const lang = ctx?.lang || Lang.en
|
||||
const pathname = ctx?.pathname || "/"
|
||||
redirect(`/${lang}/login?redirectTo=${encodeURIComponent(pathname)}`)
|
||||
}
|
||||
}
|
||||
|
||||
throw internalServerError()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user