Skip to content

Commit acfaf9b

Browse files
authored
Merge pull request #1283 from ydb-platform/err-description
added description to error message if scan failed
2 parents cf3246e + 03ad17f commit acfaf9b

File tree

9 files changed

+151
-72
lines changed

9 files changed

+151
-72
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added description to scan errors with use query service client scanner
2+
13
## v3.74.1
24
* Allowed the use of DSN without specifying the protocol/scheme
35
* Allowed casts from signed YDB types to unsigned destination types if source value is not negative

internal/query/scanner/indexed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (s IndexedScanner) Scan(dst ...interface{}) (err error) {
2929
for i := range dst {
3030
v := s.data.seekByIndex(i)
3131
if err := value.CastTo(v, dst[i]); err != nil {
32-
return xerrors.WithStackTrace(err)
32+
return xerrors.WithStackTrace(fmt.Errorf("scan error on column index %d: %w", i, err))
3333
}
3434
}
3535

internal/query/scanner/indexed_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,28 @@ func TestIndexedCastFailed(t *testing.T) {
556556
err := scanner.Scan(&A)
557557
require.ErrorIs(t, err, value.ErrCannotCast)
558558
}
559+
560+
func TestIndexedCastFailedErrMsg(t *testing.T) {
561+
scanner := Indexed(Data(
562+
[]*Ydb.Column{
563+
{
564+
Name: "a",
565+
Type: &Ydb.Type{
566+
Type: &Ydb.Type_TypeId{
567+
TypeId: Ydb.Type_UTF8,
568+
},
569+
},
570+
},
571+
},
572+
[]*Ydb.Value{
573+
{
574+
Value: &Ydb.Value_TextValue{
575+
TextValue: "test",
576+
},
577+
},
578+
},
579+
))
580+
var A uint64
581+
err := scanner.Scan(&A)
582+
require.ErrorContains(t, err, "scan error on column index 0: cast failed")
583+
}

internal/query/scanner/named.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s NamedScanner) ScanNamed(dst ...NamedDestination) (err error) {
5757
return xerrors.WithStackTrace(err)
5858
}
5959
if err = value.CastTo(v, dst[i].Ref()); err != nil {
60-
return xerrors.WithStackTrace(err)
60+
return xerrors.WithStackTrace(fmt.Errorf("scan error on column name '%s': %w", dst[i].Name(), err))
6161
}
6262
}
6363

internal/query/scanner/named_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,28 @@ func TestNamedCastFailed(t *testing.T) {
699699
err := scanner.ScanNamed(NamedRef("a", &A))
700700
require.ErrorIs(t, err, value.ErrCannotCast)
701701
}
702+
703+
func TestNamedCastFailedErrMsg(t *testing.T) {
704+
scanner := Named(Data(
705+
[]*Ydb.Column{
706+
{
707+
Name: "a",
708+
Type: &Ydb.Type{
709+
Type: &Ydb.Type_TypeId{
710+
TypeId: Ydb.Type_UTF8,
711+
},
712+
},
713+
},
714+
},
715+
[]*Ydb.Value{
716+
{
717+
Value: &Ydb.Value_TextValue{
718+
TextValue: "test",
719+
},
720+
},
721+
},
722+
))
723+
var A uint64
724+
err := scanner.ScanNamed(NamedRef("a", &A))
725+
require.ErrorContains(t, err, "scan error on column name 'a': cast failed")
726+
}

internal/query/scanner/struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s StructScanner) ScanStruct(dst interface{}, opts ...ScanStructOption) (er
6161
missingColumns = append(missingColumns, name)
6262
} else {
6363
if err = value.CastTo(v, ptr.Elem().Field(i).Addr().Interface()); err != nil {
64-
return xerrors.WithStackTrace(err)
64+
return xerrors.WithStackTrace(fmt.Errorf("scan error on struct field name '%s': %w", name, err))
6565
}
6666
existingFields[name] = struct{}{}
6767
}

internal/query/scanner/struct_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,33 @@ func TestStructCastFailed(t *testing.T) {
563563
require.ErrorIs(t, err, value.ErrCannotCast)
564564
}
565565

566+
func TestStructCastFailedErrMsg(t *testing.T) {
567+
scanner := Struct(Data(
568+
[]*Ydb.Column{
569+
{
570+
Name: "A",
571+
Type: &Ydb.Type{
572+
Type: &Ydb.Type_TypeId{
573+
TypeId: Ydb.Type_UTF8,
574+
},
575+
},
576+
},
577+
},
578+
[]*Ydb.Value{
579+
{
580+
Value: &Ydb.Value_TextValue{
581+
TextValue: "test",
582+
},
583+
},
584+
},
585+
))
586+
var row struct {
587+
A uint64
588+
}
589+
err := scanner.ScanStruct(&row)
590+
require.ErrorContains(t, err, "scan error on struct field name 'A': cast failed")
591+
}
592+
566593
func TestStructNotFoundColumns(t *testing.T) {
567594
scanner := Struct(Data(
568595
[]*Ydb.Column{

internal/value/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package value
33
import "errors"
44

55
var (
6-
ErrCannotCast = errors.New("cannot cast")
6+
ErrCannotCast = errors.New("cast failed")
77
errDestinationTypeIsNotAPointer = errors.New("destination type is not a pointer")
88
errNilDestination = errors.New("destination is nil")
99
)

0 commit comments

Comments
 (0)