mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
all: use %w instead of %v to wrap errors (#1275)
* all: use %w instead of %v for nested errors with fmt.Errorf * all: update generated code to use %w instead of %v for error wrapping
This commit is contained in:
committed by
GitHub
parent
51d19b8e5b
commit
c53b45ddb0
@@ -160,7 +160,7 @@ func CreateUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
|
||||
SetName("a8m").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed creating user: %v", err)
|
||||
return nil, fmt.Errorf("failed creating user: %w", err)
|
||||
}
|
||||
log.Println("user was created: ", u)
|
||||
return u, nil
|
||||
@@ -190,7 +190,7 @@ func QueryUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
|
||||
// or more than 1 user returned.
|
||||
Only(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed querying user: %v", err)
|
||||
return nil, fmt.Errorf("failed querying user: %w", err)
|
||||
}
|
||||
log.Println("user returned: ", u)
|
||||
return u, nil
|
||||
@@ -269,7 +269,7 @@ func CreateCars(ctx context.Context, client *ent.Client) (*ent.User, error) {
|
||||
SetRegisteredAt(time.Now()).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed creating car: %v", err)
|
||||
return nil, fmt.Errorf("failed creating car: %w", err)
|
||||
}
|
||||
log.Println("car was created: ", tesla)
|
||||
|
||||
@@ -280,7 +280,7 @@ func CreateCars(ctx context.Context, client *ent.Client) (*ent.User, error) {
|
||||
SetRegisteredAt(time.Now()).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed creating car: %v", err)
|
||||
return nil, fmt.Errorf("failed creating car: %w", err)
|
||||
}
|
||||
log.Println("car was created: ", ford)
|
||||
|
||||
@@ -292,7 +292,7 @@ func CreateCars(ctx context.Context, client *ent.Client) (*ent.User, error) {
|
||||
AddCars(tesla, ford).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed creating user: %v", err)
|
||||
return nil, fmt.Errorf("failed creating user: %w", err)
|
||||
}
|
||||
log.Println("user was created: ", a8m)
|
||||
return a8m, nil
|
||||
@@ -310,7 +310,7 @@ import (
|
||||
func QueryCars(ctx context.Context, a8m *ent.User) error {
|
||||
cars, err := a8m.QueryCars().All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed querying user cars: %v", err)
|
||||
return fmt.Errorf("failed querying user cars: %w", err)
|
||||
}
|
||||
log.Println("returned cars:", cars)
|
||||
|
||||
@@ -319,7 +319,7 @@ func QueryCars(ctx context.Context, a8m *ent.User) error {
|
||||
Where(car.ModelEQ("Ford")).
|
||||
Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed querying user cars: %v", err)
|
||||
return fmt.Errorf("failed querying user cars: %w", err)
|
||||
}
|
||||
log.Println(ford)
|
||||
return nil
|
||||
@@ -374,13 +374,13 @@ import (
|
||||
func QueryCarUsers(ctx context.Context, a8m *ent.User) error {
|
||||
cars, err := a8m.QueryCars().All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed querying user cars: %v", err)
|
||||
return fmt.Errorf("failed querying user cars: %w", err)
|
||||
}
|
||||
// Query the inverse edge.
|
||||
for _, ca := range cars {
|
||||
owner, err := ca.QueryOwner().Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed querying car %q owner: %v", ca.Model, err)
|
||||
return fmt.Errorf("failed querying car %q owner: %w", ca.Model, err)
|
||||
}
|
||||
log.Printf("car %q owner: %q\n", ca.Model, owner.Name)
|
||||
}
|
||||
@@ -542,7 +542,7 @@ Now when we have a graph with data, we can run a few queries on it:
|
||||
QueryCars(). // (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Mazda, RegisteredAt=<Time>),)
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting cars: %v", err)
|
||||
return fmt.Errorf("failed getting cars: %w", err)
|
||||
}
|
||||
log.Println("cars returned:", cars)
|
||||
// Output: (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Mazda, RegisteredAt=<Time>),)
|
||||
@@ -580,7 +580,7 @@ Now when we have a graph with data, we can run a few queries on it:
|
||||
). //
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting cars: %v", err)
|
||||
return fmt.Errorf("failed getting cars: %w", err)
|
||||
}
|
||||
log.Println("cars returned:", cars)
|
||||
// Output: (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Ford, RegisteredAt=<Time>),)
|
||||
@@ -604,7 +604,7 @@ Now when we have a graph with data, we can run a few queries on it:
|
||||
Where(group.HasUsers()).
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting groups: %v", err)
|
||||
return fmt.Errorf("failed getting groups: %w", err)
|
||||
}
|
||||
log.Println("groups returned:", groups)
|
||||
// Output: (Group(Name=GitHub), Group(Name=GitLab),)
|
||||
|
||||
@@ -227,17 +227,17 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
// Expect operation to fail, because viewer-context
|
||||
// is missing (first mutation rule check).
|
||||
if _, err := client.User.Create().Save(ctx); !errors.Is(err, privacy.Deny) {
|
||||
return fmt.Errorf("expect operation to fail, but got %v", err)
|
||||
return fmt.Errorf("expect operation to fail, but got %w", err)
|
||||
}
|
||||
// Apply the same operation with "Admin" role.
|
||||
admin := viewer.NewContext(ctx, viewer.UserViewer{Role: viewer.Admin})
|
||||
if _, err := client.User.Create().Save(admin); err != nil {
|
||||
return fmt.Errorf("expect operation to pass, but got %v", err)
|
||||
return fmt.Errorf("expect operation to pass, but got %w", err)
|
||||
}
|
||||
// Apply the same operation with "ViewOnly" role.
|
||||
viewOnly := viewer.NewContext(ctx, viewer.UserViewer{Role: viewer.View})
|
||||
if _, err := client.User.Create().Save(viewOnly); !errors.Is(err, privacy.Deny) {
|
||||
return fmt.Errorf("expect operation to fail, but got %v", err)
|
||||
return fmt.Errorf("expect operation to fail, but got %w", err)
|
||||
}
|
||||
// Allow all viewers to query users.
|
||||
for _, ctx := range []context.Context{ctx, viewOnly, admin} {
|
||||
@@ -259,7 +259,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
// Bind a privacy decision to the context (bypass all other rules).
|
||||
allow := privacy.DecisionContext(ctx, privacy.Allow)
|
||||
if _, err := client.User.Create().Save(allow); err != nil {
|
||||
return fmt.Errorf("expect operation to pass, but got %v", err)
|
||||
return fmt.Errorf("expect operation to pass, but got %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -330,23 +330,23 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
// Expect operation to fail, because viewer-context
|
||||
// is missing (first mutation rule check).
|
||||
if _, err := client.Tenant.Create().Save(ctx); !errors.Is(err, privacy.Deny) {
|
||||
return fmt.Errorf("expect operation to fail, but got %v", err)
|
||||
return fmt.Errorf("expect operation to fail, but got %w", err)
|
||||
}
|
||||
// Deny tenant creation if the viewer is not admin.
|
||||
viewOnly := viewer.NewContext(ctx, viewer.UserViewer{Role: viewer.View})
|
||||
if _, err := client.Tenant.Create().Save(viewOnly); !errors.Is(err, privacy.Deny) {
|
||||
return fmt.Errorf("expect operation to fail, but got %v", err)
|
||||
return fmt.Errorf("expect operation to fail, but got %w", err)
|
||||
}
|
||||
// Apply the same operation with "Admin" role.
|
||||
admin := viewer.NewContext(ctx, viewer.UserViewer{Role: viewer.Admin})
|
||||
hub, err := client.Tenant.Create().SetName("GitHub").Save(admin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("expect operation to pass, but got %v", err)
|
||||
return fmt.Errorf("expect operation to pass, but got %w", err)
|
||||
}
|
||||
fmt.Println(hub)
|
||||
lab, err := client.Tenant.Create().SetName("GitLab").Save(admin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("expect operation to pass, but got %v", err)
|
||||
return fmt.Errorf("expect operation to pass, but got %w", err)
|
||||
}
|
||||
fmt.Println(lab)
|
||||
return nil
|
||||
@@ -470,7 +470,7 @@ func DenyMismatchedTenants() privacy.MutationRule {
|
||||
// and it matches the tenant-id of the group above.
|
||||
uid, err := m.Client().User.Query().Where(user.IDIn(users...)).QueryTenant().OnlyID(ctx)
|
||||
if err != nil {
|
||||
return privacy.Denyf("querying the tenant-id %v", err)
|
||||
return privacy.Denyf("querying the tenant-id %w", err)
|
||||
}
|
||||
if uid != tid {
|
||||
return privacy.Denyf("mismatch tenant-ids for group/users %d != %d", tid, uid)
|
||||
@@ -514,7 +514,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
}
|
||||
entgo, err := client.Group.Create().SetName("entgo.io").SetTenant(hub).AddUsers(a8m).Save(admin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("expect operation to pass, but got %v", err)
|
||||
return fmt.Errorf("expect operation to pass, but got %w", err)
|
||||
}
|
||||
fmt.Println(entgo)
|
||||
return nil
|
||||
@@ -556,7 +556,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
}
|
||||
entgo, err = entgo.Update().SetName("entgo").Save(hubView)
|
||||
if err != nil {
|
||||
return fmt.Errorf("expect operation to pass, but got %v", err)
|
||||
return fmt.Errorf("expect operation to pass, but got %w", err)
|
||||
}
|
||||
fmt.Println(entgo)
|
||||
return nil
|
||||
|
||||
@@ -221,7 +221,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetName("Mashraki").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating user: %v", err)
|
||||
return fmt.Errorf("creating user: %w", err)
|
||||
}
|
||||
log.Println("user:", a8m)
|
||||
card1, err := client.Card.
|
||||
@@ -231,21 +231,21 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetExpired(time.Now().Add(time.Minute)).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating card: %v", err)
|
||||
return fmt.Errorf("creating card: %w", err)
|
||||
}
|
||||
log.Println("card:", card1)
|
||||
// Only returns the card of the user,
|
||||
// and expects that there's only one.
|
||||
card2, err := a8m.QueryCard().Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying card: %v", err)
|
||||
return fmt.Errorf("querying card: %w", err)
|
||||
}
|
||||
log.Println("card:", card2)
|
||||
// The Card entity is able to query its owner using
|
||||
// its back-reference.
|
||||
owner, err := card2.QueryOwner().Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying owner: %v", err)
|
||||
return fmt.Errorf("querying owner: %w", err)
|
||||
}
|
||||
log.Println("owner:", owner)
|
||||
return nil
|
||||
@@ -303,7 +303,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetValue(1).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating the head: %v", err)
|
||||
return fmt.Errorf("creating the head: %w", err)
|
||||
}
|
||||
curr := head
|
||||
// Generate the following linked-list: 1<->2<->3<->4<->5.
|
||||
@@ -340,7 +340,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
// Check that the change actually applied:
|
||||
prev, err := head.QueryPrev().Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting head's prev: %v", err)
|
||||
return fmt.Errorf("getting head's prev: %w", err)
|
||||
}
|
||||
fmt.Printf("\n%v", prev.Value == tail.Value)
|
||||
// Output: true
|
||||
@@ -380,7 +380,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetName("a8m").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating user: %v", err)
|
||||
return fmt.Errorf("creating user: %w", err)
|
||||
}
|
||||
nati, err := client.User.
|
||||
Create().
|
||||
@@ -389,7 +389,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetSpouse(a8m).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating user: %v", err)
|
||||
return fmt.Errorf("creating user: %w", err)
|
||||
}
|
||||
|
||||
// Query the spouse edge.
|
||||
@@ -466,14 +466,14 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetName("pedro").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating pet: %v", err)
|
||||
return fmt.Errorf("creating pet: %w", err)
|
||||
}
|
||||
lola, err := client.Pet.
|
||||
Create().
|
||||
SetName("lola").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating pet: %v", err)
|
||||
return fmt.Errorf("creating pet: %w", err)
|
||||
}
|
||||
// Create the user, and add its pets on the creation.
|
||||
a8m, err := client.User.
|
||||
@@ -483,7 +483,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
AddPets(pedro, lola).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating user: %v", err)
|
||||
return fmt.Errorf("creating user: %w", err)
|
||||
}
|
||||
fmt.Println("User created:", a8m)
|
||||
// Output: User(id=1, age=30, name=a8m)
|
||||
@@ -553,7 +553,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
SetValue(2).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating the root: %v", err)
|
||||
return fmt.Errorf("creating the root: %w", err)
|
||||
}
|
||||
// Add additional nodes to the tree:
|
||||
//
|
||||
@@ -673,7 +673,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
QueryGroups().
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying a8m groups: %v", err)
|
||||
return fmt.Errorf("querying a8m groups: %w", err)
|
||||
}
|
||||
fmt.Println(groups)
|
||||
// Output: [Group(id=1, name=GitHub) Group(id=2, name=GitLab)]
|
||||
@@ -682,7 +682,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
QueryGroups().
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying nati groups: %v", err)
|
||||
return fmt.Errorf("querying nati groups: %w", err)
|
||||
}
|
||||
fmt.Println(groups)
|
||||
// Output: [Group(id=1, name=GitHub)]
|
||||
@@ -696,7 +696,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
QueryUsers(). // [a8m, nati]
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("traversing the graph: %v", err)
|
||||
return fmt.Errorf("traversing the graph: %w", err)
|
||||
}
|
||||
fmt.Println(users)
|
||||
// Output: [User(id=1, age=30, name=a8m) User(id=2, age=28, name=nati)]
|
||||
|
||||
@@ -10,14 +10,14 @@ title: Transactions
|
||||
func GenTx(ctx context.Context, client *ent.Client) error {
|
||||
tx, err := client.Tx(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("starting a transaction: %v", err)
|
||||
return fmt.Errorf("starting a transaction: %w", err)
|
||||
}
|
||||
hub, err := tx.Group.
|
||||
Create().
|
||||
SetName("Github").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return rollback(tx, fmt.Errorf("failed creating the group: %v", err))
|
||||
return rollback(tx, fmt.Errorf("failed creating the group: %w", err))
|
||||
}
|
||||
// Create the admin of the group.
|
||||
dan, err := tx.User.
|
||||
@@ -52,7 +52,7 @@ func GenTx(ctx context.Context, client *ent.Client) error {
|
||||
// with the rollback error if occurred.
|
||||
func rollback(tx *ent.Tx, err error) error {
|
||||
if rerr := tx.Rollback(); rerr != nil {
|
||||
err = fmt.Errorf("%v: %v", err, rerr)
|
||||
err = fmt.Errorf("%w: %v", err, rerr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ func Gen(ctx context.Context, client *ent.Client) error {
|
||||
SetName("Github").
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed creating the group: %v", err)
|
||||
return fmt.Errorf("failed creating the group: %w", err)
|
||||
}
|
||||
// Create the admin of the group.
|
||||
// Unlike `Save`, `SaveX` panics if an error occurs.
|
||||
@@ -175,7 +175,7 @@ func Traverse(ctx context.Context, client *ent.Client) error {
|
||||
QueryOwner(). // Coco's owner: Alex.
|
||||
Only(ctx) // Expect only one entity to return in the query.
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed querying the owner: %v", err)
|
||||
return fmt.Errorf("failed querying the owner: %w", err)
|
||||
}
|
||||
fmt.Println(owner)
|
||||
// Output:
|
||||
@@ -204,7 +204,7 @@ func Traverse(ctx context.Context, client *ent.Client) error {
|
||||
).
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed querying the pets: %v", err)
|
||||
return fmt.Errorf("failed querying the pets: %w", err)
|
||||
}
|
||||
fmt.Println(pets)
|
||||
// Output:
|
||||
|
||||
Reference in New Issue
Block a user