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:
Matthew Gabeler-Lee
2021-03-03 13:05:33 -05:00
committed by GitHub
parent 51d19b8e5b
commit c53b45ddb0
191 changed files with 461 additions and 461 deletions

View File

@@ -25,7 +25,7 @@ type ColumnScanner interface {
func ScanOne(rows ColumnScanner, v interface{}) error {
columns, err := rows.Columns()
if err != nil {
return fmt.Errorf("sql/scan: failed getting column names: %v", err)
return fmt.Errorf("sql/scan: failed getting column names: %w", err)
}
if n := len(columns); n != 1 {
return fmt.Errorf("sql/scan: unexpected number of columns: %d", n)
@@ -85,7 +85,7 @@ func ScanValue(rows ColumnScanner) (driver.Value, error) {
func ScanSlice(rows ColumnScanner, v interface{}) error {
columns, err := rows.Columns()
if err != nil {
return fmt.Errorf("sql/scan: failed getting column names: %v", err)
return fmt.Errorf("sql/scan: failed getting column names: %w", err)
}
rv := reflect.ValueOf(v)
switch {
@@ -111,7 +111,7 @@ func ScanSlice(rows ColumnScanner, v interface{}) error {
for rows.Next() {
values := scan.values()
if err := rows.Scan(values...); err != nil {
return fmt.Errorf("sql/scan: failed scanning rows: %v", err)
return fmt.Errorf("sql/scan: failed scanning rows: %w", err)
}
vv := reflect.Append(rv, scan.value(values...))
rv.Set(vv)

View File

@@ -197,7 +197,7 @@ func (m *Migrate) txCreate(ctx context.Context, tx dialect.Tx, tables ...*Table)
default: // !exist
query, args := m.tBuilder(t).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create table %q: %v", t.Name, err)
return fmt.Errorf("create table %q: %w", t.Name, err)
}
// If global unique identifier is enabled and it's not
// a relation table, allocate a range for the table pk.
@@ -210,7 +210,7 @@ func (m *Migrate) txCreate(ctx context.Context, tx dialect.Tx, tables ...*Table)
for _, idx := range t.Indexes {
query, args := m.addIndex(idx, t.Name).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create index %q: %v", idx.Name, err)
return fmt.Errorf("create index %q: %w", idx.Name, err)
}
}
}
@@ -243,7 +243,7 @@ func (m *Migrate) txCreate(ctx context.Context, tx dialect.Tx, tables ...*Table)
}
query, args := b.Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create foreign keys for %q: %v", t.Name, err)
return fmt.Errorf("create foreign keys for %q: %w", t.Name, err)
}
}
return nil
@@ -262,7 +262,7 @@ func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change
}
for _, idx := range change.index.drop {
if err := m.dropIndex(ctx, tx, idx, table); err != nil {
return fmt.Errorf("drop index of table %q: %v", table, err)
return fmt.Errorf("drop index of table %q: %w", table, err)
}
}
}
@@ -275,13 +275,13 @@ func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change
for i := range queries {
query, args := queries[i].Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("alter table %q: %v", table, err)
return fmt.Errorf("alter table %q: %w", table, err)
}
}
for _, idx := range change.index.add {
query, args := m.addIndex(idx, table).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create index %q: %v", table, err)
return fmt.Errorf("create index %q: %w", table, err)
}
}
return nil
@@ -409,7 +409,7 @@ func (m *Migrate) fixture(ctx context.Context, tx dialect.Tx, curr, new *Table)
for _, fk := range new.ForeignKeys {
ok, err := m.fkExist(ctx, tx, fk.Symbol)
if err != nil {
return fmt.Errorf("checking foreign-key existence %q: %v", fk.Symbol, err)
return fmt.Errorf("checking foreign-key existence %q: %w", fk.Symbol, err)
}
if !ok {
continue
@@ -424,7 +424,7 @@ func (m *Migrate) fixture(ctx context.Context, tx dialect.Tx, curr, new *Table)
}
query, args := d.renameColumn(curr, &Column{Name: column}, newcol).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("rename column %q: %v", column, err)
return fmt.Errorf("rename column %q: %w", column, err)
}
prev, ok := curr.column(column)
if !ok {
@@ -441,7 +441,7 @@ func (m *Migrate) fixture(ctx context.Context, tx dialect.Tx, curr, new *Table)
idx2 := &Index{Name: newcol.Name, Unique: true, Columns: []*Column{newcol}}
query, args := d.renameIndex(curr, idx, idx2).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("rename index %q: %v", prev.Name, err)
return fmt.Errorf("rename index %q: %w", prev.Name, err)
}
idx.Name = idx2.Name
default:
@@ -464,7 +464,7 @@ func (m *Migrate) fixture(ctx context.Context, tx dialect.Tx, curr, new *Table)
if idx.sameAs(idx2) {
query, args := d.renameIndex(curr, idx, idx2).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("rename index %q: %v", idx.Name, err)
return fmt.Errorf("rename index %q: %w", idx.Name, err)
}
idx.Name = idx2.Name
break Find
@@ -500,7 +500,7 @@ func (m *Migrate) types(ctx context.Context, tx dialect.Tx) error {
AddColumn(&Column{Name: "type", Type: field.TypeString, Unique: true})
query, args := m.tBuilder(t).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create types table: %v", err)
return fmt.Errorf("create types table: %w", err)
}
return nil
}
@@ -508,7 +508,7 @@ func (m *Migrate) types(ctx context.Context, tx dialect.Tx) error {
query, args := sql.Dialect(m.Dialect()).
Select("type").From(sql.Table(TypeTable)).OrderBy(sql.Asc("id")).Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return fmt.Errorf("query types table: %v", err)
return fmt.Errorf("query types table: %w", err)
}
defer rows.Close()
return sql.ScanSlice(rows, &m.typeRanges)
@@ -525,7 +525,7 @@ func (m *Migrate) allocPKRange(ctx context.Context, tx dialect.Tx, t *Table) err
query, args := sql.Dialect(m.Dialect()).
Insert(TypeTable).Columns("type").Values(t.Name).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("insert into type: %v", err)
return fmt.Errorf("insert into type: %w", err)
}
id = len(m.typeRanges)
m.typeRanges = append(m.typeRanges, t.Name)
@@ -552,12 +552,12 @@ func (m *Migrate) fkColumn(ctx context.Context, tx dialect.Tx, fk *ForeignKey) (
Query()
rows := &sql.Rows{}
if err := tx.Query(ctx, query, args, rows); err != nil {
return "", fmt.Errorf("reading foreign-key %q column: %v", fk.Symbol, err)
return "", fmt.Errorf("reading foreign-key %q column: %w", fk.Symbol, err)
}
defer rows.Close()
column, err := sql.ScanString(rows)
if err != nil {
return "", fmt.Errorf("scanning foreign-key %q column: %v", fk.Symbol, err)
return "", fmt.Errorf("scanning foreign-key %q column: %w", fk.Symbol, err)
}
return column, nil
}
@@ -604,9 +604,9 @@ func (m *Migrate) symbol(name string) string {
// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
func rollback(tx dialect.Tx, err error) error {
err = fmt.Errorf("sql/schema: %v", err)
err = fmt.Errorf("sql/schema: %w", err)
if rerr := tx.Rollback(); rerr != nil {
err = fmt.Errorf("%s: %v", err.Error(), rerr)
err = fmt.Errorf("%w: %v", err, rerr)
}
return err
}
@@ -615,7 +615,7 @@ func rollback(tx dialect.Tx, err error) error {
func exist(ctx context.Context, tx dialect.Tx, query string, args ...interface{}) (bool, error) {
rows := &sql.Rows{}
if err := tx.Query(ctx, query, args, rows); err != nil {
return false, fmt.Errorf("reading schema information %v", err)
return false, fmt.Errorf("reading schema information %w", err)
}
defer rows.Close()
n, err := sql.ScanInt(rows)

View File

@@ -28,7 +28,7 @@ type MySQL struct {
func (d *MySQL) init(ctx context.Context, tx dialect.Tx) error {
rows := &sql.Rows{}
if err := tx.Query(ctx, "SHOW VARIABLES LIKE 'version'", []interface{}{}, rows); err != nil {
return fmt.Errorf("mysql: querying mysql version %v", err)
return fmt.Errorf("mysql: querying mysql version %w", err)
}
defer rows.Close()
if !rows.Next() {
@@ -39,7 +39,7 @@ func (d *MySQL) init(ctx context.Context, tx dialect.Tx) error {
}
version := make([]string, 2)
if err := rows.Scan(&version[0], &version[1]); err != nil {
return fmt.Errorf("mysql: scanning mysql version: %v", err)
return fmt.Errorf("mysql: scanning mysql version: %w", err)
}
d.version = version[1]
return nil
@@ -74,7 +74,7 @@ func (d *MySQL) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
sql.EQ("TABLE_NAME", name)),
).Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("mysql: reading table description %v", err)
return nil, fmt.Errorf("mysql: reading table description %w", err)
}
// Call Close in cases of failures (Close is idempotent).
defer rows.Close()
@@ -82,7 +82,7 @@ func (d *MySQL) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
for rows.Next() {
c := &Column{}
if err := d.scanColumn(c, rows); err != nil {
return nil, fmt.Errorf("mysql: %v", err)
return nil, fmt.Errorf("mysql: %w", err)
}
if c.PrimaryKey() {
t.PrimaryKey = append(t.PrimaryKey, c)
@@ -93,7 +93,7 @@ func (d *MySQL) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
return nil, err
}
if err := rows.Close(); err != nil {
return nil, fmt.Errorf("mysql: closing rows %v", err)
return nil, fmt.Errorf("mysql: closing rows %w", err)
}
indexes, err := d.indexes(ctx, tx, name)
if err != nil {
@@ -123,12 +123,12 @@ func (d *MySQL) indexes(ctx context.Context, tx dialect.Tx, name string) ([]*Ind
OrderBy("index_name", "seq_in_index").
Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("mysql: reading index description %v", err)
return nil, fmt.Errorf("mysql: reading index description %w", err)
}
defer rows.Close()
idx, err := d.scanIndexes(rows)
if err != nil {
return nil, fmt.Errorf("mysql: %v", err)
return nil, fmt.Errorf("mysql: %w", err)
}
return idx, nil
}
@@ -150,13 +150,13 @@ func (d *MySQL) verifyRange(ctx context.Context, tx dialect.Tx, t *Table, expect
)).
Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return fmt.Errorf("mysql: query auto_increment %v", err)
return fmt.Errorf("mysql: query auto_increment %w", err)
}
// Call Close in cases of failures (Close is idempotent).
defer rows.Close()
actual := &sql.NullInt64{}
if err := sql.ScanOne(rows, actual); err != nil {
return fmt.Errorf("mysql: scan auto_increment %v", err)
return fmt.Errorf("mysql: scan auto_increment %w", err)
}
if err := rows.Close(); err != nil {
return err
@@ -363,7 +363,7 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
defaults sql.NullString
)
if err := rows.Scan(&c.Name, &c.typ, &nullable, &c.Key, &defaults, &c.Attr, &sql.NullString{}, &sql.NullString{}); err != nil {
return fmt.Errorf("scanning column description: %v", err)
return fmt.Errorf("scanning column description: %w", err)
}
c.Unique = c.UniqueKey()
if nullable.Valid {
@@ -470,7 +470,7 @@ func (d *MySQL) scanIndexes(rows *sql.Rows) (Indexes, error) {
seqindex int
)
if err := rows.Scan(&name, &column, &nonuniq, &seqindex); err != nil {
return nil, fmt.Errorf("scanning index description: %v", err)
return nil, fmt.Errorf("scanning index description: %w", err)
}
// Ignore primary keys.
if name == "PRIMARY" {
@@ -581,7 +581,7 @@ func (d *MySQL) normalizeJSON(ctx context.Context, tx dialect.Tx, t *Table) erro
)).
Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return fmt.Errorf("mysql: query table constraints %v", err)
return fmt.Errorf("mysql: query table constraints %w", err)
}
// Call Close in cases of failures (Close is idempotent).
defer rows.Close()
@@ -630,7 +630,7 @@ func parseColumn(typ string) (parts []string, size int64, unsigned bool, err err
size, err = strconv.ParseInt(parts[1], 10, 64)
}
if err != nil {
return parts, size, unsigned, fmt.Errorf("converting %s size to int: %v", parts[0], err)
return parts, size, unsigned, fmt.Errorf("converting %s size to int: %w", parts[0], err)
}
return parts, size, unsigned, nil
}
@@ -651,7 +651,7 @@ func (d *MySQL) fkNames(ctx context.Context, tx dialect.Tx, table, column string
rows = &sql.Rows{}
)
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("mysql: reading constraint names %v", err)
return nil, fmt.Errorf("mysql: reading constraint names %w", err)
}
defer rows.Close()
if err := sql.ScanSlice(rows, &names); err != nil {

View File

@@ -26,7 +26,7 @@ type Postgres struct {
func (d *Postgres) init(ctx context.Context, tx dialect.Tx) error {
rows := &sql.Rows{}
if err := tx.Query(ctx, "SHOW server_version_num", []interface{}{}, rows); err != nil {
return fmt.Errorf("querying server version %v", err)
return fmt.Errorf("querying server version %w", err)
}
defer rows.Close()
if !rows.Next() {
@@ -37,7 +37,7 @@ func (d *Postgres) init(ctx context.Context, tx dialect.Tx) error {
}
var version string
if err := rows.Scan(&version); err != nil {
return fmt.Errorf("scanning version: %v", err)
return fmt.Errorf("scanning version: %w", err)
}
if len(version) < 6 {
return fmt.Errorf("malformed version: %s", version)
@@ -95,7 +95,7 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl
sql.EQ("table_name", name),
)).Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("postgres: reading table description %v", err)
return nil, fmt.Errorf("postgres: reading table description %w", err)
}
// Call `Close` in cases of failures (`Close` is idempotent).
defer rows.Close()
@@ -111,7 +111,7 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl
return nil, err
}
if err := rows.Close(); err != nil {
return nil, fmt.Errorf("closing rows %v", err)
return nil, fmt.Errorf("closing rows %w", err)
}
idxs, err := d.indexes(ctx, tx, name)
if err != nil {
@@ -183,7 +183,7 @@ func (d *Postgres) indexes(ctx context.Context, tx dialect.Tx, table string) (In
rows := &sql.Rows{}
query, args := d.indexesQuery(table)
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("querying indexes for table %s: %v", table, err)
return nil, fmt.Errorf("querying indexes for table %s: %w", table, err)
}
defer rows.Close()
var (
@@ -197,7 +197,7 @@ func (d *Postgres) indexes(ctx context.Context, tx dialect.Tx, table string) (In
unique, primary bool
)
if err := rows.Scan(&name, &column, &primary, &unique, &seqindex); err != nil {
return nil, fmt.Errorf("scanning index description: %v", err)
return nil, fmt.Errorf("scanning index description: %w", err)
}
// If the index is prefixed with the table, it may was added by
// `addIndex` and it should be trimmed. But, since entc prefixes
@@ -229,7 +229,7 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
udt sql.NullString
)
if err := rows.Scan(&c.Name, &c.typ, &nullable, &defaults, &udt); err != nil {
return fmt.Errorf("scanning column description: %v", err)
return fmt.Errorf("scanning column description: %w", err)
}
if nullable.Valid {
c.Nullable = nullable.String == "YES"

View File

@@ -215,37 +215,37 @@ func (c *Column) ScanDefault(value string) error {
case c.IntType():
v := &sql.NullInt64{}
if err := v.Scan(value); err != nil {
return fmt.Errorf("scanning int value for column %q: %v", c.Name, err)
return fmt.Errorf("scanning int value for column %q: %w", c.Name, err)
}
c.Default = v.Int64
case c.UintType():
v := &sql.NullInt64{}
if err := v.Scan(value); err != nil {
return fmt.Errorf("scanning uint value for column %q: %v", c.Name, err)
return fmt.Errorf("scanning uint value for column %q: %w", c.Name, err)
}
c.Default = uint64(v.Int64)
case c.FloatType():
v := &sql.NullFloat64{}
if err := v.Scan(value); err != nil {
return fmt.Errorf("scanning float value for column %q: %v", c.Name, err)
return fmt.Errorf("scanning float value for column %q: %w", c.Name, err)
}
c.Default = v.Float64
case c.Type == field.TypeBool:
v := &sql.NullBool{}
if err := v.Scan(value); err != nil {
return fmt.Errorf("scanning bool value for column %q: %v", c.Name, err)
return fmt.Errorf("scanning bool value for column %q: %w", c.Name, err)
}
c.Default = v.Bool
case c.Type == field.TypeString || c.Type == field.TypeEnum:
v := &sql.NullString{}
if err := v.Scan(value); err != nil {
return fmt.Errorf("scanning string value for column %q: %v", c.Name, err)
return fmt.Errorf("scanning string value for column %q: %w", c.Name, err)
}
c.Default = v.String
case c.Type == field.TypeJSON:
v := &sql.NullString{}
if err := v.Scan(value); err != nil {
return fmt.Errorf("scanning json value for column %q: %v", c.Name, err)
return fmt.Errorf("scanning json value for column %q: %w", c.Name, err)
}
c.Default = v.String
default:

View File

@@ -24,7 +24,7 @@ type SQLite struct {
func (d *SQLite) init(ctx context.Context, tx dialect.Tx) error {
on, err := exist(ctx, tx, "PRAGMA foreign_keys")
if err != nil {
return fmt.Errorf("sqlite: check foreign_keys pragma: %v", err)
return fmt.Errorf("sqlite: check foreign_keys pragma: %w", err)
}
if !on {
// foreign_keys pragma is off, either enable it by execute "PRAGMA foreign_keys=ON"
@@ -159,7 +159,7 @@ func (d *SQLite) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
OrderBy("pk").
Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("sqlite: reading table description %v", err)
return nil, fmt.Errorf("sqlite: reading table description %w", err)
}
// Call Close in cases of failures (Close is idempotent).
defer rows.Close()
@@ -167,7 +167,7 @@ func (d *SQLite) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
for rows.Next() {
c := &Column{}
if err := d.scanColumn(c, rows); err != nil {
return nil, fmt.Errorf("sqlite: %v", err)
return nil, fmt.Errorf("sqlite: %w", err)
}
if c.PrimaryKey() {
t.PrimaryKey = append(t.PrimaryKey, c)
@@ -178,7 +178,7 @@ func (d *SQLite) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
return nil, err
}
if err := rows.Close(); err != nil {
return nil, fmt.Errorf("sqlite: closing rows %v", err)
return nil, fmt.Errorf("sqlite: closing rows %w", err)
}
indexes, err := d.indexes(ctx, tx, name)
if err != nil {
@@ -211,7 +211,7 @@ func (d *SQLite) indexes(ctx context.Context, tx dialect.Tx, name string) (Index
From(sql.Table(fmt.Sprintf("pragma_index_list('%s')", name)).Unquote()).
Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("reading table indexes %v", err)
return nil, fmt.Errorf("reading table indexes %w", err)
}
defer rows.Close()
var idx Indexes
@@ -219,7 +219,7 @@ func (d *SQLite) indexes(ctx context.Context, tx dialect.Tx, name string) (Index
i := &Index{}
origin := sql.NullString{}
if err := rows.Scan(&i.Name, &i.Unique, &origin); err != nil {
return nil, fmt.Errorf("scanning index description %v", err)
return nil, fmt.Errorf("scanning index description %w", err)
}
i.primary = origin.String == "pk"
idx = append(idx, i)
@@ -228,7 +228,7 @@ func (d *SQLite) indexes(ctx context.Context, tx dialect.Tx, name string) (Index
return nil, err
}
if err := rows.Close(); err != nil {
return nil, fmt.Errorf("closing rows %v", err)
return nil, fmt.Errorf("closing rows %w", err)
}
for i := range idx {
columns, err := d.indexColumns(ctx, tx, idx[i].Name)
@@ -253,7 +253,7 @@ func (d *SQLite) indexColumns(ctx context.Context, tx dialect.Tx, name string) (
OrderBy("seqno").
Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return nil, fmt.Errorf("reading table indexes %v", err)
return nil, fmt.Errorf("reading table indexes %w", err)
}
defer rows.Close()
var names []string
@@ -271,7 +271,7 @@ func (d *SQLite) scanColumn(c *Column, rows *sql.Rows) error {
defaults sql.NullString
)
if err := rows.Scan(&c.Name, &c.typ, &notnull, &defaults, &pk); err != nil {
return fmt.Errorf("scanning column description: %v", err)
return fmt.Errorf("scanning column description: %w", err)
}
c.Nullable = notnull.Int64 == 0
if pk.Int64 > 0 {

View File

@@ -704,11 +704,11 @@ func (u *updater) nodes(ctx context.Context, tx dialect.ExecQuerier) (int, error
query, args := selector.Query()
rows := &sql.Rows{}
if err := u.tx.Query(ctx, query, args, rows); err != nil {
return 0, fmt.Errorf("querying table %s: %v", u.Node.Table, err)
return 0, fmt.Errorf("querying table %s: %w", u.Node.Table, err)
}
defer rows.Close()
if err := sql.ScanSlice(rows, &ids); err != nil {
return 0, fmt.Errorf("scan node ids: %v", err)
return 0, fmt.Errorf("scan node ids: %w", err)
}
if err := rows.Close(); err != nil {
return 0, err
@@ -833,7 +833,7 @@ func (u *updater) scan(rows *sql.Rows) error {
return err
}
if err := rows.Scan(values...); err != nil {
return fmt.Errorf("failed scanning rows: %v", err)
return fmt.Errorf("failed scanning rows: %w", err)
}
if err := u.Assign(columns, values); err != nil {
return err
@@ -857,7 +857,7 @@ func (c *creator) node(ctx context.Context, tx dialect.ExecQuerier) error {
return err
}
if err := c.insert(ctx, tx, insert); err != nil {
return fmt.Errorf("insert node to table %q: %v", c.Table, err)
return fmt.Errorf("insert node to table %q: %w", c.Table, err)
}
if err := c.graph.addM2MEdges(ctx, []driver.Value{c.ID.Value}, edges[M2M]); err != nil {
return err
@@ -916,7 +916,7 @@ func (c *creator) nodes(ctx context.Context, tx dialect.ExecQuerier) error {
insert.Values(vs...)
}
if err := c.batchInsert(ctx, tx, insert); err != nil {
return fmt.Errorf("insert nodes to table %q: %v", c.Nodes[0].Table, err)
return fmt.Errorf("insert nodes to table %q: %w", c.Nodes[0].Table, err)
}
if err := c.batchAddM2M(ctx, c.BatchCreateSpec); err != nil {
return err
@@ -1051,7 +1051,7 @@ func (g *graph) clearM2MEdges(ctx context.Context, ids []driver.Value, edges Edg
}
query, args := deleter.Query()
if err := g.tx.Exec(ctx, query, args, &res); err != nil {
return fmt.Errorf("remove m2m edge for table %s: %v", table, err)
return fmt.Errorf("remove m2m edge for table %s: %w", table, err)
}
}
return nil
@@ -1086,7 +1086,7 @@ func (g *graph) addM2MEdges(ctx context.Context, ids []driver.Value, edges EdgeS
}
query, args := insert.Query()
if err := g.tx.Exec(ctx, query, args, &res); err != nil {
return fmt.Errorf("add m2m edge for table %s: %v", table, err)
return fmt.Errorf("add m2m edge for table %s: %w", table, err)
}
}
return nil
@@ -1129,7 +1129,7 @@ func (g *graph) batchAddM2M(ctx context.Context, spec *BatchCreateSpec) error {
query, args = tables[table].Query()
)
if err := g.tx.Exec(ctx, query, args, &res); err != nil {
return fmt.Errorf("add m2m edge for table %s: %v", table, err)
return fmt.Errorf("add m2m edge for table %s: %w", table, err)
}
}
return nil
@@ -1152,7 +1152,7 @@ func (g *graph) clearFKEdges(ctx context.Context, ids []driver.Value, edges []*E
Query()
var res sql.Result
if err := g.tx.Exec(ctx, query, args, &res); err != nil {
return fmt.Errorf("add %s edge for table %s: %v", edge.Rel, edge.Table, err)
return fmt.Errorf("add %s edge for table %s: %w", edge.Rel, edge.Table, err)
}
}
return nil
@@ -1182,7 +1182,7 @@ func (g *graph) addFKEdges(ctx context.Context, ids []driver.Value, edges []*Edg
Query()
var res sql.Result
if err := g.tx.Exec(ctx, query, args, &res); err != nil {
return fmt.Errorf("add %s edge for table %s: %v", edge.Rel, edge.Table, err)
return fmt.Errorf("add %s edge for table %s: %w", edge.Rel, edge.Table, err)
}
affected, err := res.RowsAffected()
if err != nil {
@@ -1204,7 +1204,7 @@ func setTableColumns(fields []*FieldSpec, edges map[Rel][]*EdgeSpec, set func(st
if fi.Type == field.TypeJSON {
buf, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("marshal value for column %s: %v", fi.Column, err)
return fmt.Errorf("marshal value for column %s: %w", fi.Column, err)
}
// If the underlying driver does not support JSON types,
// driver.DefaultParameterConverter will convert it to uint8.
@@ -1286,7 +1286,7 @@ func insertLastIDs(ctx context.Context, tx dialect.ExecQuerier, insert *sql.Inse
// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
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)
}
return err
}