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
@@ -114,7 +114,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.
|
||||
@@ -184,7 +184,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:
|
||||
@@ -213,7 +213,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:
|
||||
@@ -273,7 +273,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.
|
||||
@@ -283,21 +283,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
|
||||
@@ -355,7 +355,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.
|
||||
@@ -392,7 +392,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
|
||||
@@ -432,7 +432,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().
|
||||
@@ -441,7 +441,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.
|
||||
@@ -518,14 +518,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.
|
||||
@@ -535,7 +535,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)
|
||||
@@ -605,7 +605,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:
|
||||
//
|
||||
@@ -725,7 +725,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)]
|
||||
@@ -734,7 +734,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)]
|
||||
@@ -748,7 +748,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)]
|
||||
|
||||
Reference in New Issue
Block a user