Hi guys! I've been trying to handle an specific er...
# orm-help
e
Hi guys! I've been trying to handle an specific error with the code below
Copy code
try {
    await db.category.create({ data: fields })
    return json({ success: true })
  } catch (error) {
    return badRequest({
      type: typeof error,
      instance: error.constructor.name,
      constructorName: Prisma.PrismaClientKnownRequestError.name,
      isInstance: error instanceof Prisma.PrismaClientKnownRequestError,
    })
    // if (error instanceof Prisma.PrismaClientKnownRequestError) {
    //   if (error.code === 'P2002') {
    //     return badRequest('Category already exists')
    //   }
    // }
    // return badRequest(error.message)
  }
But the operation
error instance of Prisma.PrismaClientKnownRequestError
returns
false
Anyone know what I could be missing? The response I'm getting:
Copy code
constructorName: "PrismaClientKnownRequestError"
instance: "PrismaClientKnownRequestError"
isInstance: false
type: "object"
n
Hey Eme šŸ‘‹ Could you show the output of the error? I think this should work because this is similar to the official example that we have listed in Handling exceptions and errors This is the working example we have:
Copy code
import { PrismaClient, Prisma } from '@prisma/client'

const client = new PrismaClient()

try {
  await client.user.create({ data: { email: '<mailto:alreadyexisting@mail.com|alreadyexisting@mail.com>' } })
} catch (e) {
  if (e instanceof Prisma.PrismaClientKnownRequestError) {
    // The .code property can be accessed in a type-safe manner
    if (e.code === 'P2002') {
      console.log(
        'There is a unique constraint violation, a new user cannot be created with this email'
      )
    }
  }
  throw e
}
e
The output of the error is
Copy code
Error: 
Invalid `prisma.category.create()` invocation:


  Unique constraint failed on the fields: (`slug`)
    at Object.request
Just to close this thread: Issue was with the framework in development mode build, everything worked as expected in production mode
šŸ‘ 1
n
Thanks for getting back, can you let us know which framework had this issue?