Skip to content

Commit

Permalink
adapt inline docs
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Oct 28, 2023
1 parent 59256dd commit 834a3d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
19 changes: 19 additions & 0 deletions generator/templates/errors.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ var ErrNotFound = types.ErrNotFound

type ErrUniqueConstraint = types.ErrUniqueConstraint[prismaFields]

// IsUniqueConstraint 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.UniqueConstraintError(); 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 IsUniqueConstraint(err error) (*types.ErrUniqueConstraint[prismaFields], bool) {
return types.CheckUniqueConstraint[prismaFields](err)
}
9 changes: 0 additions & 9 deletions runtime/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ type ErrUniqueConstraint[T F] struct {
}

// CheckUniqueConstraint 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.UniqueConstraintError(); err != nil {
// log.Printf("unique constraint on the field: %s", info.Field)
// }
// }
//
// Ideally this will be replaced with Prisma-generated errors in the future
func CheckUniqueConstraint[T F](err error) (*ErrUniqueConstraint[T], bool) {
var ufr *protocol.UserFacingError
Expand Down

0 comments on commit 834a3d3

Please sign in to comment.