Похожие чаты

Initially I did this so the errors don't leak to

the client (in case they contain sensitive data), but something in my guts is telling me this is stupid to catch the error and throw it again (although I've seen a similar pattern somewhere else)

Any recommendations?

export async function fetchFilteredPacks(query: string, currentPage: number) {
  try {
    const authResult = await auth();

    if (!authResult?.user) throw new AuthError();

    const skip = (currentPage - 1) * TAKE;

    const data = await db.pack.findMany({
      orderBy: { updatedAt: "desc" },
      where: {
        OR: [
          { description: { contains: query } },
          { name: { contains: query } },
          { url: { contains: query } },
        ],
      },
      take: TAKE,
      skip: skip,
    });

    return data;
  } catch (error) {
    console.error(error);

    if (error instanceof AuthError) throw new Error("Unauthorized");

    throw new Error("Failed to fetch packs");
  }
}

7 ответов

25 просмотров

You can just isolate the auth part, otherwise it's fine

I might move the try catch outside this function

Anon- Автор вопроса
Thomas
You can just isolate the auth part, otherwise it's...

Do you mean that I should put it in its own try-catch and rethrow or return if the user isn't signed in?

Anon
Do you mean that I should put it in its own try-ca...

Like move the auth call and check to before the try catch

Anon- Автор вопроса
Thomas
I might move the try catch outside this function

I'm using Next.js, and currently this function is used in my /packs route that is already checking for the auth state and also handles the errors, but I wanted to future proof it just in case I use it in another place and I want it to throw but not leak sensitive data

Keep fetchFilteredPacks as it was, make a new one like: authAndFetchFilteredPacksHideErrors

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта