Next.js Discord

Discord Forum

How to handle erros using next-intl without do it inside catch block?

Unanswered
English Angora posted this in #help-forum
Open in Discord
English AngoraOP
I want to translate some prisma errors, but the fact that getTranslations is async, inside my error handling that return a translated message, I'll got a promise/async inside a catch block, for me it is a bad way to handle the error messages, but I don't know another way to solve it, can you guys help me?
A little of my code:

export const prismaClientKnownRequestErrorHandler = async (error: Prisma.PrismaClientKnownRequestError) => {
  const locale = await getServerLocale(); // custom func to get locale from server
  const t = await getTranslations({ locale, namespace: 'prisma' }); // here is the problem, the handler need to be async to await this getTranslations
  switch (error.code) {
    case 'P2000':
      return NextResponse.json({ error, message: t('P2000') }, { status: 400 }); // and here is where I call the translator to get a message from each code error
    case 'P2001':
...

and this is the error handler calling:

async function fail<T>(message: string, error: T, statusCode = 500) {
  if (error instanceof Prisma.PrismaClientKnownRequestError) {
    return await prismaClientKnownRequestErrorHandler(error);
  }
...

0 Replies