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");
}
}
You can just isolate the auth part, otherwise it's fine
I might move the try catch outside this function
Do you mean that I should put it in its own try-catch and rethrow or return if the user isn't signed in?
Like move the auth call and check to before the try catch
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
Обсуждают сегодня