Improve some error messages

pull/29/head
Vinícius Garcia 2022-08-27 12:28:39 -03:00
parent 8620600d01
commit 86dd623eac
2 changed files with 18 additions and 14 deletions

28
ksql.go
View File

@ -126,7 +126,7 @@ func (c DB) Query(
slicePtr := reflect.ValueOf(records) slicePtr := reflect.ValueOf(records)
slicePtrType := slicePtr.Type() slicePtrType := slicePtr.Type()
if slicePtrType.Kind() != reflect.Ptr { if slicePtrType.Kind() != reflect.Ptr {
return fmt.Errorf("ksql: expected to receive a pointer to slice of structs, but got: %T", records) return fmt.Errorf("KSQL: expected to receive a pointer to slice of structs, but got: %T", records)
} }
sliceType := slicePtrType.Elem() sliceType := slicePtrType.Elem()
slice := slicePtr.Elem() slice := slicePtr.Elem()
@ -220,16 +220,16 @@ func (c DB) QueryOne(
v := reflect.ValueOf(record) v := reflect.ValueOf(record)
t := v.Type() t := v.Type()
if t.Kind() != reflect.Ptr { if t.Kind() != reflect.Ptr {
return fmt.Errorf("ksql: expected to receive a pointer to struct, but got: %T", record) return fmt.Errorf("KSQL: expected to receive a pointer to struct, but got: %T", record)
} }
if v.IsNil() { if v.IsNil() {
return fmt.Errorf("ksql: expected a valid pointer to struct as argument but received a nil pointer: %v", record) return fmt.Errorf("KSQL: expected a valid pointer to struct as argument but received a nil pointer: %v", record)
} }
tStruct := t.Elem() tStruct := t.Elem()
if tStruct.Kind() != reflect.Struct { if tStruct.Kind() != reflect.Struct {
return fmt.Errorf("ksql: expected to receive a pointer to struct, but got: %T", record) return fmt.Errorf("KSQL: expected to receive a pointer to struct, but got: %T", record)
} }
info, err := structs.GetTagInfo(tStruct) info, err := structs.GetTagInfo(tStruct)
@ -402,13 +402,13 @@ func (c DB) Insert(
t := v.Type() t := v.Type()
if err := assertStructPtr(t); err != nil { if err := assertStructPtr(t); err != nil {
return fmt.Errorf( return fmt.Errorf(
"ksql: expected record to be a pointer to struct, but got: %T", "KSQL: expected record to be a pointer to struct, but got: %T",
record, record,
) )
} }
if v.IsNil() { if v.IsNil() {
return fmt.Errorf("ksql: expected a valid pointer to struct as argument but received a nil pointer: %v", record) return fmt.Errorf("KSQL: expected a valid pointer to struct as argument but received a nil pointer: %v", record)
} }
if err := table.validate(); err != nil { if err := table.validate(); err != nil {
@ -595,7 +595,7 @@ func normalizeIDsAsMap(idNames []string, idOrMap interface{}) (idMap map[string]
if t.Kind() == reflect.Ptr { if t.Kind() == reflect.Ptr {
v := reflect.ValueOf(idOrMap) v := reflect.ValueOf(idOrMap)
if v.IsNil() { if v.IsNil() {
return nil, fmt.Errorf("ksql: expected a valid pointer to struct as argument but received a nil pointer: %v", idOrMap) return nil, fmt.Errorf("KSQL: expected a valid pointer to struct as argument but received a nil pointer: %v", idOrMap)
} }
t = t.Elem() t = t.Elem()
} }
@ -648,7 +648,7 @@ func (c DB) Patch(
tStruct := t tStruct := t
if t.Kind() == reflect.Ptr { if t.Kind() == reflect.Ptr {
if v.IsNil() { if v.IsNil() {
return fmt.Errorf("ksql: expected a valid pointer to struct as argument but received a nil pointer: %v", record) return fmt.Errorf("KSQL: expected a valid pointer to struct as argument but received a nil pointer: %v", record)
} }
tStruct = t.Elem() tStruct = t.Elem()
} }
@ -943,14 +943,14 @@ func scanRowsFromType(
v reflect.Value, v reflect.Value,
) error { ) error {
if t.Kind() != reflect.Ptr { if t.Kind() != reflect.Ptr {
return fmt.Errorf("ksql: expected record to be a pointer to struct, but got: %T", record) return fmt.Errorf("KSQL: expected record to be a pointer to struct, but got: %T", record)
} }
v = v.Elem() v = v.Elem()
t = t.Elem() t = t.Elem()
if t.Kind() != reflect.Struct { if t.Kind() != reflect.Struct {
return fmt.Errorf("ksql: expected record to be a pointer to struct, but got: %T", record) return fmt.Errorf("KSQL: expected record to be a pointer to struct, but got: %T", record)
} }
info, err := structs.GetTagInfo(t) info, err := structs.GetTagInfo(t)
@ -970,14 +970,18 @@ func scanRowsFromType(
} else { } else {
names, err := rows.Columns() names, err := rows.Columns()
if err != nil { if err != nil {
return err return fmt.Errorf("KSQL: unable to read columns from returned rows: %w", err)
} }
// Since this version uses the names of the columns it works // Since this version uses the names of the columns it works
// with any order of attributes/columns. // with any order of attributes/columns.
scanArgs = getScanArgsFromNames(dialect, names, v, info) scanArgs = getScanArgsFromNames(dialect, names, v, info)
} }
return rows.Scan(scanArgs...) err = rows.Scan(scanArgs...)
if err != nil {
return fmt.Errorf("KSQL: scan error: %w", err)
}
return nil
} }
func getScanArgsForNestedStructs(dialect Dialect, rows Rows, t reflect.Type, v reflect.Value, info structs.StructInfo) ([]interface{}, error) { func getScanArgsForNestedStructs(dialect Dialect, rows Rows, t reflect.Type, v reflect.Value, info structs.StructInfo) ([]interface{}, error) {

View File

@ -508,7 +508,7 @@ func QueryTest(
var users []user var users []user
err := c.Query(ctx, &users, `SELECT * FROM users`) err := c.Query(ctx, &users, `SELECT * FROM users`)
tt.AssertErrContains(t, err, "fakeScanErr") tt.AssertErrContains(t, err, "KSQL", "scan error", "fakeScanErr")
}) })
t.Run("should report error if DBAdapter.Err() returns an error", func(t *testing.T) { t.Run("should report error if DBAdapter.Err() returns an error", func(t *testing.T) {
@ -2644,7 +2644,7 @@ func ScanRowsTest(
var u map[string]interface{} var u map[string]interface{}
err = scanRows(dialect, rows, &u) err = scanRows(dialect, rows, &u)
tt.AssertErrContains(t, err, "ksql", "expected", "pointer to struct", "map[string]interface") tt.AssertErrContains(t, err, "KSQL", "expected", "pointer to struct", "map[string]interface")
}) })
}) })
} }