is it correct to have an error like this ?
Unanswered
Briard posted this in #help-forum
BriardOP
I want to handle errors, for example when executing this mutation an error appears
server router
"use client";
etc
const mutationAdd = trpc.user.addUser.useMutation({
onSuccess: async (data) => {
await utils.user.getUsers.refetch();
router.push("/admin/all-employees");
},
onError: (error) => {
toast({
variant: "destructive",
action: <ToastAction altText="Try again">{error.message}</ToastAction>,
});
return;
},
});
server router
addUser: publicProcedure
.input(
z.object({
username: z.string(),
name: z.string(),
email: z.string().email(),
password: z
.string()
.min(5, "password should be at least 5 digits long."),
role: z.string(),
})
)
.mutation(async ({ input }) => {
const newUser = await auth.api.createUser({
headers: await headers(),
body: {
name: input.name,
email: input.email,
password: input.password,
role: input.role,
data: {
username: input.username,
},
},
});
return newUser;
}),
1 Reply
@Briard I want to handle errors, for example when executing this mutation an error appears
tsx
"use client";
etc
const mutationAdd = trpc.user.addUser.useMutation({
onSuccess: async (data) => {
await utils.user.getUsers.refetch();
router.push("/admin/all-employees");
},
onError: (error) => {
toast({
variant: "destructive",
action: <ToastAction altText="Try again">{error.message}</ToastAction>,
});
return;
},
});
server router
tsx
addUser: publicProcedure
.input(
z.object({
username: z.string(),
name: z.string(),
email: z.string().email(),
password: z
.string()
.min(5, "password should be at least 5 digits long."),
role: z.string(),
})
)
.mutation(async ({ input }) => {
const newUser = await auth.api.createUser({
headers: await headers(),
body: {
name: input.name,
email: input.email,
password: input.password,
role: input.role,
data: {
username: input.username,
},
},
});
return newUser;
}),
Black Turnstone
Yes, http status code error appear in console even if you handle them in onError function.
And I believe that uncaught TRPCClientError is console loged from httpBatchLink function not sure about that.
And I believe that uncaught TRPCClientError is console loged from httpBatchLink function not sure about that.