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
@@ -75,7 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) {
|
||||
}
|
||||
tx, err := newTx(ctx, c.driver)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %v", err)
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %w", err)
|
||||
}
|
||||
cfg := c.config
|
||||
cfg.driver = tx
|
||||
@@ -97,7 +97,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
|
||||
BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
|
||||
}).BeginTx(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %v", err)
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %w", err)
|
||||
}
|
||||
cfg := c.config
|
||||
cfg.driver = &txDriver{tx: tx, drv: c.driver}
|
||||
|
||||
@@ -265,7 +265,7 @@ func isSQLConstraintError(err error) (*ConstraintError, bool) {
|
||||
// rollback calls tx.Rollback and wraps the given error with the rollback error if present.
|
||||
func rollback(tx dialect.Tx, err error) error {
|
||||
if rerr := tx.Rollback(); rerr != nil {
|
||||
err = fmt.Errorf("%s: %v", err.Error(), rerr)
|
||||
err = fmt.Errorf("%w: %v", err, rerr)
|
||||
}
|
||||
if err, ok := isSQLConstraintError(err); ok {
|
||||
return err
|
||||
|
||||
@@ -460,7 +460,7 @@ func (gq *GroupQuery) sqlAll(ctx context.Context) ([]*Group, error) {
|
||||
},
|
||||
}
|
||||
if err := sqlgraph.QueryEdges(ctx, gq.driver, _spec); err != nil {
|
||||
return nil, fmt.Errorf(`query edges "users": %v`, err)
|
||||
return nil, fmt.Errorf(`query edges "users": %w`, err)
|
||||
}
|
||||
query.Where(user.IDIn(edgeids...))
|
||||
neighbors, err := query.All(ctx)
|
||||
@@ -515,7 +515,7 @@ func (gq *GroupQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
func (gq *GroupQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
n, err := gq.sqlCount(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("ent: check existence: %v", err)
|
||||
return false, fmt.Errorf("ent: check existence: %w", err)
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} }
|
||||
func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
|
||||
migrate, err := schema.NewMigrate(s.drv, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ent/migrate: %v", err)
|
||||
return fmt.Errorf("ent/migrate: %w", err)
|
||||
}
|
||||
return migrate.Create(ctx, Tables...)
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.Migrat
|
||||
}
|
||||
migrate, err := schema.NewMigrate(drv, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ent/migrate: %v", err)
|
||||
return fmt.Errorf("ent/migrate: %w", err)
|
||||
}
|
||||
return migrate.Create(ctx, Tables...)
|
||||
}
|
||||
|
||||
@@ -460,7 +460,7 @@ func (pq *PetQuery) sqlAll(ctx context.Context) ([]*Pet, error) {
|
||||
},
|
||||
}
|
||||
if err := sqlgraph.QueryEdges(ctx, pq.driver, _spec); err != nil {
|
||||
return nil, fmt.Errorf(`query edges "friends": %v`, err)
|
||||
return nil, fmt.Errorf(`query edges "friends": %w`, err)
|
||||
}
|
||||
query.Where(pet.IDIn(edgeids...))
|
||||
neighbors, err := query.All(ctx)
|
||||
@@ -515,7 +515,7 @@ func (pq *PetQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
func (pq *PetQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
n, err := pq.sqlCount(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("ent: check existence: %v", err)
|
||||
return false, fmt.Errorf("ent: check existence: %w", err)
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
@@ -554,7 +554,7 @@ func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
|
||||
},
|
||||
}
|
||||
if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
|
||||
return nil, fmt.Errorf(`query edges "friends": %v`, err)
|
||||
return nil, fmt.Errorf(`query edges "friends": %w`, err)
|
||||
}
|
||||
query.Where(user.IDIn(edgeids...))
|
||||
neighbors, err := query.All(ctx)
|
||||
@@ -618,7 +618,7 @@ func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
|
||||
},
|
||||
}
|
||||
if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
|
||||
return nil, fmt.Errorf(`query edges "groups": %v`, err)
|
||||
return nil, fmt.Errorf(`query edges "groups": %w`, err)
|
||||
}
|
||||
query.Where(group.IDIn(edgeids...))
|
||||
neighbors, err := query.All(ctx)
|
||||
@@ -676,7 +676,7 @@ func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
n, err := uq.sqlCount(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("ent: check existence: %v", err)
|
||||
return false, fmt.Errorf("ent: check existence: %w", err)
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
@@ -67,7 +67,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.
|
||||
@@ -127,7 +127,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:
|
||||
@@ -148,7 +148,7 @@ func Traverse2(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:
|
||||
@@ -160,14 +160,14 @@ func Traverse2(ctx context.Context, client *ent.Client) error {
|
||||
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.
|
||||
@@ -237,7 +237,7 @@ func WithTx(ctx context.Context, client *ent.Client, fn func(tx *ent.Tx) error)
|
||||
// rollback calls to tx.Rollback and wraps the given 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user