Skip to content

Commit

Permalink
add errors docs
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Nov 12, 2023
1 parent f3749e3 commit 17f1865
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/pages/docs/reference/client/errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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
log.Printf("unique constraint on the `user.name` field")
}

// For MySQL and MongoDB, use the constraint key
log.Printf("unique constraint on the key: %s", info.Key)
}
}
```

0 comments on commit 17f1865

Please sign in to comment.