I am super new to Prisma so apologies so please be gentle
Prisma allows you to set formatting for errors generated by Prisma by adding an object to the Prisma contructor.
export const db = new PrismaClient({
errorFormat: 'pretty'
})
So I am using this in a super simple page that I am building.
So I have a ContactPage , that shows 2 things:
- ContactsCell and
- ContactsForm
ContactsCell is based off of Contact Schema as follows:
model Contact {
id Int @id @default(autoincrement())
name String
email String
message String
createdAt DateTime @default(now())
@@unique([ name, email, message ])
}
Within the contact from I have created the following mutation code
mutation CreateContactMutation($input: CreateContactInput!) {
createContact(input: $input) {
id
name
email
message
createdAt
}
}
`
const [ create , { loading, error }] = useMutation(CREATE_CONTACT, {
refetchQueries: [{ query: QUERY }],
awaitRefetchQueries: true
})
and finally onsubmit has something like this:
const onSubmit = (input) => {
console.log(input)
create({ variables: { input }})
formMethods.reset()
}
and the contract form has a FormError component that gets the error passed from the useMutation hook.
<FormError className="alert alert-danger" error={error} />
However, the error message from Prisma that I get isnt formatted properly and the message looks something like this:
e[31mInvalid
e[1m`prisma.contact.create()`
e[22m invocation:
e[39m Unique constraint failed on the fields: (`name`,`email`,`message`)
My question is how do I go about formatting this error from Prisma?