diff --git a/docs/pages/docs/reference/client/errors.md b/docs/pages/docs/reference/client/errors.md new file mode 100644 index 000000000..fb0d6df0c --- /dev/null +++ b/docs/pages/docs/reference/client/errors.md @@ -0,0 +1,39 @@ +# Errors + +## ErrNotFound + +`ErrNotFound` is returned when a query does not return any results. This error may be returned in `FindUnique`, `FindFirst`, but also when updating or deleting single records using `FindUnique().Update()` and `FindUnique().Delete()`. + +```go +post, err := client.Post.FindFirst( + db.Post.Title.Equals("hi"), +).Exec(ctx) +if err != nil { + if errors.Is(err, db.ErrNotFound) { + panic("no record with title 'hi' found") + } + panic("error occurred: %s", err) +} +``` + +## IsUniqueConstraintViolation + +A unique constraint violation happens when a query attempts to insert or update a record with a value that already exists in the database, or in other words, violates a unique constraint. + +```go +user, err := db.User.CreateOne(...).Exec(cxt) +if err != nil { + if info, err := db.IsErrUniqueConstraint(); err != nil { + // Fields exists for Postgres and SQLite + log.Printf("unique constraint on the fields: %s", info.Fields) + + // you can also compare it with generated field names: + if info.Fields[0] == db.User.Name.Field() { + // do something + } + + // For MySQL and MongoDB, use the constraint key + log.Printf("unique constraint on the key: %s", info.Key) + } +} +```