#can't catch prisma error in loader

1 messages · Page 1 of 1 (latest)

indigo warren
#

I've got a difficult error in a Remix loader. A prisma query seems to be throwing an error, but it's not being caught by my try/catch block. Here is the code:

try {
  console.log('here');
  const matchingRecord = await prisma.adminInvite.findFirst({
    where: {
      email: result.data.email,
      token: result.data.token,
    },
  });
  console.log('here2');
} catch (error) {
  console.log('here3');
}

When this part of my code gets executed, the server output is as follows:

here
GET /app/admin/accept-invite?invite=54abc7771a76dabd4fbfd7f6211211995c6c61f731ff9e110719c05c56c4bdf7be505a7260e9a60aaa1c299b0da9496e3890b392bbbcbe448d024db6a91a1ae76696dcf399dc9927cef7a0492f919f59d6b3d185d1b93fd37ca3c74dc86416d5dd178f1cc4fe143563c60cc60de26e3360321f1fa1b9cd56d087a23d57290088d07964f7dc3267e25c4fc5cb15a548abb592dc7938924ccc018a4fd48353c1bf559d6cf0a821457f8c7a0f3aa008b3cd 302 - - 26.915 ms
UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<NodeResponse>".

It never seems to even get inside the catch block, and I have no idea why.

tall pike
#

can you provide the full code? the invite redirects user to somewhere?

graceful dock
indigo warren
# tall pike can you provide the full code? the invite redirects user to somewhere?
export async function loader({ request }: DataFunctionArgs) {
  const user = requireUser(request);
  const url = new URL(request.url);
  const params = new URLSearchParams(url.search);
  const invite = params.get('invite');
  if (invite) {
    const decryptedInvite = await decrypt(invite);
    const parsedInvite = JSON.parse(decryptedInvite);
    const result = await schema.safeParse(parsedInvite);
    if (result.success === true) {
      try {
        console.log('here');
        const matchingRecord = await prisma.adminInvite.findFirst({
          where: {
            email: result.data.email,
            token: result.data.token,
            expirationDate: {
              gte: new Date(),
            },
          },
        });
        console.log({ matchingRecord });
        if (matchingRecord) {
          await prisma.user.update({
            where: { email: result.data.email },
            data: { role: 'admin' },
          });
          await prisma.adminInvite.delete({
            where: { id: matchingRecord.id },
          });
          return redirect('/app/admin/admins');
        } else {
          throw new Error('Invalid invite');
        }
      } catch (error) {
        console.log({ error });
      }
    }
  }
  return { user };
}

As I said earlier, it displays the log message before the prisma call, but nothing after, and doesn't give me a chance to catch any error.

tall pike
#

Throw redirect if no auth cookie?

indigo warren
#

It outputs "here" but nothing afterwards.

tall pike
#

How about the console.log(matchingRecord)?

indigo warren
tall pike
#

wait gte

#

I guess that's the point?

#

Normally when working with dates, I'll just use date fns to compare or do anything related

#

Since the date object is complicated

#

How about the result.data.email

#

And result.data.token?

indigo warren
#

It looks like my problem may have been the lack of an await before the requireUser;

#

Thanks so much for your help troubleshooting this