-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errors): add unique constraint violation error (#1069)
- Loading branch information
Showing
18 changed files
with
366 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); 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) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,29 @@ | ||
{{- /*gotype:github.com/steebchen/prisma-client-go/generator.Root*/ -}} | ||
|
||
var ErrNotFound = types.ErrNotFound | ||
var IsErrNotFound = types.IsErrNotFound | ||
|
||
type ErrUniqueConstraint = types.ErrUniqueConstraint[prismaFields] | ||
|
||
// IsErrUniqueConstraint returns on a unique constraint error or violation with error info | ||
// Use as follows: | ||
// | ||
// user, err := db.User.CreateOne(...).Exec(cxt) | ||
// if err != nil { | ||
// if info, err := db.IsErrUniqueConstraint(err); 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, use the constraint key | ||
// log.Printf("unique constraint on the key: %s", info.Key) | ||
// } | ||
// } | ||
// | ||
func IsErrUniqueConstraint(err error) (*types.ErrUniqueConstraint[prismaFields], bool) { | ||
return types.CheckUniqueConstraint[prismaFields](err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{{- /*gotype:github.com/steebchen/prisma-client-go/generator.Root*/ -}} | ||
|
||
type prismaFields string | ||
|
||
{{ range $model := $.AST.Models }} | ||
type {{ $model.Name.GoLowerCase }}PrismaFields = prismaFields | ||
|
||
{{ range $field := $model.Fields }} | ||
const {{ $model.Name.GoLowerCase }}Field{{ $field.Name.GoCase }} {{ $model.Name.GoLowerCase }}PrismaFields = "{{ $field.Name }}" | ||
{{ end }} | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
module github.com/steebchen/prisma-client-go | ||
|
||
go 1.16 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/joho/godotenv v1.5.1 | ||
github.com/shopspring/decimal v1.3.1 | ||
github.com/stretchr/testify v1.8.4 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,69 @@ | ||
package types | ||
|
||
import "errors" | ||
import ( | ||
"errors" | ||
|
||
"github.com/steebchen/prisma-client-go/engine/protocol" | ||
) | ||
|
||
// ErrNotFound gets returned when a database record does not exist | ||
var ErrNotFound = errors.New("ErrNotFound") | ||
|
||
// IsErrNotFound is true if the error is a ErrNotFound, which gets returned when a database record does not exist | ||
// This can happen when you call `FindUnique` on a record, or update or delete a single record which doesn't exist. | ||
func IsErrNotFound(err error) bool { | ||
return errors.Is(err, ErrNotFound) | ||
} | ||
|
||
type F interface { | ||
~string | ||
} | ||
|
||
type ErrUniqueConstraint[T F] struct { | ||
// Message is the error message | ||
Message string | ||
// Fields only shows on Postgres | ||
Fields []T | ||
// Key only shows on MySQL | ||
Key string | ||
} | ||
|
||
// CheckUniqueConstraint returns on a unique constraint error or violation with error info | ||
// Ideally this will be replaced with Prisma-generated errors in the future | ||
func CheckUniqueConstraint[T F](err error) (*ErrUniqueConstraint[T], bool) { | ||
if err == nil { | ||
return nil, false | ||
} | ||
|
||
var ufr *protocol.UserFacingError | ||
if ok := errors.As(err, &ufr); !ok { | ||
return nil, false | ||
} | ||
|
||
if ufr.ErrorCode != "P2002" { | ||
return nil, false | ||
} | ||
|
||
// postgres | ||
if items, ok := ufr.Meta.Target.([]interface{}); ok { | ||
var fields []T | ||
for _, f := range items { | ||
field, ok := f.(string) | ||
if ok { | ||
fields = append(fields, T(field)) | ||
} | ||
} | ||
return &ErrUniqueConstraint[T]{ | ||
Fields: fields, | ||
}, true | ||
} | ||
|
||
// mysql | ||
if item, ok := ufr.Meta.Target.(string); ok { | ||
return &ErrUniqueConstraint[T]{ | ||
Key: item, | ||
}, true | ||
} | ||
|
||
return nil, false | ||
} |
Oops, something went wrong.