Error formatting

Overview

By default, Prisma Client uses ANSI escape characters to pretty print the error stack and give recommendations on how to fix a problem. While this is very useful when using Prisma Client from the terminal, in contexts like a GraphQL API, you only want the minimal error without any additional formatting.

This page explains how error formatting can be configured with Prisma Client.

Formatting levels

There are 3 error formatting levels:

  1. Pretty Error (default): Includes a full stack trace with colors, syntax highlighting of the code and extended error message with a possible solution for the problem.
  2. Colorless Error: Same as pretty errors, just without colors.
  3. Minimal Error: The raw error message.

In order to configure these different error formatting levels, there are two options:

  • Setting the config options via environment variables
  • Providing the config options to the PrismaClient constructor

Formatting via environment variables

  • NO_COLOR: If this env var is provided, colors are stripped from the error message. Therefore you end up with a colorless error. The NO_COLOR environment variable is a standard described here.
  • NODE_ENV=production: If the env var NODE_ENV is set to production, only the minimal error will be printed. This allows for easier digestion of logs in production environments.

Formatting via the PrismaClient constructor

The constructor argument to control the error formatting is called errorFormat. It can have the following values:

  • undefined: If it's not defined, the default is colorless.
  • pretty: Enables pretty error formatting.
  • colorless: Enables colorless error formatting.
  • minimal: Enables minimal error formatting.

It can be used like so:

const prisma = new PrismaClient({
errorFormat: 'pretty',
})
const prisma = new PrismaClient({
errorFormat: 'colorless',
})
const prisma = new PrismaClient({
errorFormat: 'minimal',
})

As the errorFormat property is optional, you still can just instantiate Prisma Client like this:

const prisma = new PrismaClient()
Edit this page on GitHub