Error Handling in Prisma

From the Prisma ORM cheat sheet · Error Handling & Optimization · verified Jul 2026

Error Handling

Handle Prisma-specific errors and implement proper error recovery

typescript
import { Prisma } from '@prisma/client'

// Handle specific Prisma errors
try {
  await prisma.user.create({
    data: { email: 'user@example.com' }
  })
} catch (error) {
  if (error instanceof Prisma.PrismaClientKnownRequestError) {
    if (error.code === 'P2002') {
      console.log('Unique constraint violation:', error.meta?.target)
    } else if (error.code === 'P2025') {
      console.log('Record not found')
    }
  } else if (error instanceof Prisma.PrismaClientValidationError) {
    console.log('Validation error:', error.message)
  } else {
    throw error
  }
}
💡 Check error.code for specific error types
⚠️ P2002 includes constraint name in meta.target
📌 Implement retry logic for connection errors
✅ Log errors but don't expose internals to users

More Prisma tasks

Back to the full Prisma ORM cheat sheet