Skip to content

Commit

Permalink
adapt naming
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Nov 12, 2023
1 parent ffb3c25 commit f3749e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions generator/templates/errors.gotpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{{- /*gotype:github.com/steebchen/prisma-client-go/generator.Root*/ -}}

var ErrNotFound = types.ErrNotFound
var IsErrNotFound = types.IsErrNotFound

type ErrUniqueConstraint = types.ErrUniqueConstraint[prismaFields]

// IsUniqueConstraintErr returns on a unique constraint error or violation with error info
// 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.IsUniqueConstraintErr(); 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)
//
Expand All @@ -23,6 +24,6 @@ type ErrUniqueConstraint = types.ErrUniqueConstraint[prismaFields]
// }
// }
//
func IsUniqueConstraintErr(err error) (*types.ErrUniqueConstraint[prismaFields], bool) {
func IsErrUniqueConstraint(err error) (*types.ErrUniqueConstraint[prismaFields], bool) {
return types.CheckUniqueConstraint[prismaFields](err)
}
6 changes: 6 additions & 0 deletions runtime/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
// 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
}
Expand Down
10 changes: 5 additions & 5 deletions test/errors/unique/unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestUniqueConstraintViolation(t *testing.T) {
User.Username.Set("username"),
).Exec(ctx)

violation, ok := IsUniqueConstraintErr(err)
violation, ok := IsErrUniqueConstraint(err)
// assert.Equal(t, &ErrUniqueConstraint{
// Field: User.Email.Field(),
// }, violation)
Expand All @@ -58,7 +58,7 @@ func TestUniqueConstraintViolation(t *testing.T) {
User.Username.Set("username"),
).Exec(ctx)

violation, ok := IsUniqueConstraintErr(err)
violation, ok := IsErrUniqueConstraint(err)
// assert.Equal(t, &ErrUniqueConstraint{
// Key: "User_email_key",
// }, violation)
Expand All @@ -81,7 +81,7 @@ func TestUniqueConstraintViolation(t *testing.T) {
User.Username.Set("username"),
).Exec(ctx)

violation, ok := IsUniqueConstraintErr(err)
violation, ok := IsErrUniqueConstraint(err)
// assert.Equal(t, &ErrUniqueConstraint{
// Field: User.Email.Field(),
// }, violation)
Expand All @@ -104,7 +104,7 @@ func TestUniqueConstraintViolation(t *testing.T) {
User.Username.Set("username"),
).Exec(ctx)

violation, ok := IsUniqueConstraintErr(err)
violation, ok := IsErrUniqueConstraint(err)
// assert.Equal(t, &ErrUniqueConstraint{
// Key: "User_email_key",
// }, violation)
Expand All @@ -121,7 +121,7 @@ func TestUniqueConstraintViolation(t *testing.T) {
User.Username.Set("username"),
).Exec(ctx)

_, ok := IsUniqueConstraintErr(err)
_, ok := IsErrUniqueConstraint(err)

assert.Equal(t, false, ok)
},
Expand Down

0 comments on commit f3749e3

Please sign in to comment.