#Error not being throw for true conditon

4 messages · Page 1 of 1 (latest)

grizzled rain
#

Totalcount modal should be updated in case the current date is less than the last totalcount date

  try {
    const product = await prisma.product.findUnique({
      where: {
        id,
      },
    });
   const res = await prisma.$transaction(async (tx) => {
      // this one is independent
      await tx.product.update({
        where: {
          id,
        },
        data: {
          quantity: newQuantity,
        },
      });

      //
      await tx.issueItem.create({
        data: {
          productId: id,
          issuedToId: parseInt(issuedTo),
          issuedQuantity: issueQuantity,
          issuedAt: dateOfIssue,
          jobCard,
        },
      });

      let currentDate = dateOfIssue;
      // await updateTotalStockCount(issueQuantity, currentDate, operation, tx);
      // const record = await tx.totalStockCount.findMany({
      const record = await tx.totalStockCount.findMany({
        orderBy: {
          date: "desc",
        },
        take: 1,
      });
      console.log("latestrecord:", record);

      if (record.length > 0 && isBefore(currentDate, record[0].date)) {
        console.log("Current Date:", currentDate);
        console.log("before teh latest date");
        // should throw error
        return new NextResponse("ERROR issuing item", { status: 401 });
      }
    });

    return NextResponse.json("Issued item successfully", { status: 201 });
  } catch (error) {
    console.log("ERROR: ", error);
    return new NextResponse("SOMETHIHG WENT WRONG", { status: 401 });
  }

the current date in the if condition if (record.length > 0 && isBefore(currentDate, record[0].date))
is being logged but stil no error is thrown why is that?

quick ivy
#

Hey @grizzled rain 👋

Based on the code snippet the return new NextResponse("ERROR issuing item", { status: 401 }); does not actually "throw" an error; it merely returns an object. If you want to stop execution and jump to the catch block, you should use throw new Error("ERROR issuing item");

grizzled rain
#

that exactly what I did

#

thanks for the replying tho