Next.js Discord

Discord Forum

Error.tsx does not catch errors

Answered
Crucian carp posted this in #help-forum
Open in Discord
Crucian carpOP
Hi guys, I'm throwing an error here, but my error.tsx doesn't seem to catch this error:
`export default async function queryDatabase(query: string, params: any[]) {
try {
const promisePool = pool.promise();
const [rows, fields] = await promisePool.execute(query, params);
return rows;
} catch (e) {
throw new Error("Could not query database");
}
}``

That is my folder structure:
Answered by Ray
error.js can't catch the error inside useEffect
View full answer

22 Replies

Crucian carpOP
Yes, it does
Crucian carpOP
you just return false here
try throw e instead of return false
Crucian carpOP
oh ok
@Crucian carp Click to see attachment
is this a page component
Crucian carpOP
It is
Crucian carpOP
Yes
error.js can't catch the error inside useEffect
Answer
nor any handler event
Crucian carpOP
Oh. How can I call the function before the page load?
turn the page to server component and call it directly
Crucian carpOP
But then I cannot use states anymore
move it down
Crucian carpOP
Oh, okay. I will try
export default async function Page() {
  await test()

  return <ClientComponent />
}

just like this
metadata need to be use with server component also, so it is recommented to leave the page with server component