PrismaP
Prisma9mo ago
3 replies
devve2kcc

Best way to handle errors

Hi, what is the best way to handle errors or non-existent values, like, is this case im blindly updating an record where the id is the id of the project, in the case of the id does not exist, prisma throw an P2025 error, that i can handle it on a try catch and return that no record has found, and nothing has been updated.

Or the other way, i can first make an query to find the project, and if he exist, i update it.

What is the common way, or the best way to use on this cases?

 

.put(
    '/:id',
    zValidator('json', ProjectSchema.pick({ name: true, description: true }).partial()),
    zValidator('param', z.object({ id: z.string() })),
    async (c) => {
      const user = c.get('user');
      const data = c.req.valid('json');
      const { id } = c.req.valid('param');

      if (!user) return c.body(null, 401);

      const response = await prisma.project.update({
        data,
        where: {
          userId: user.id,
          id,
        },
      });

      return c.json(response);
    }
  )
Was this page helpful?