Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 1cffdc1

Browse files
authored
Merge pull request #556 from kuba--/err-msg
Make conversion error clearer and less detailed.
2 parents 9ee3f1d + 499cba7 commit 1cffdc1

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

sql/type.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ var (
3737

3838
// ErrNotArray is returned when the value is not an array.
3939
ErrNotArray = errors.NewKind("value of type %T is not an array")
40+
41+
// ErrConvertToSQL is returned when Convert failed.
42+
// It makes an error less verbose comparingto what spf13/cast returns.
43+
ErrConvertToSQL = errors.NewKind("incompatible conversion to SQL type: %s")
4044
)
4145

4246
// Schema is the definition of a table.
@@ -309,7 +313,6 @@ func (t numberT) Convert(v interface{}) (interface{}, error) {
309313
default:
310314
return nil, ErrInvalidType.New(t.t)
311315
}
312-
313316
}
314317

315318
// Compare implements Type interface.
@@ -526,7 +529,11 @@ func (t textT) SQL(v interface{}) sqltypes.Value {
526529

527530
// Convert implements Type interface.
528531
func (t textT) Convert(v interface{}) (interface{}, error) {
529-
return cast.ToStringE(v)
532+
val, err := cast.ToStringE(v)
533+
if err != nil {
534+
return nil, ErrConvertToSQL.New(t)
535+
}
536+
return val, nil
530537
}
531538

532539
// Compare implements Type interface.
@@ -559,7 +566,11 @@ func (t booleanT) SQL(v interface{}) sqltypes.Value {
559566

560567
// Convert implements Type interface.
561568
func (t booleanT) Convert(v interface{}) (interface{}, error) {
562-
return cast.ToBoolE(v)
569+
val, err := cast.ToBoolE(v)
570+
if err != nil {
571+
return nil, ErrConvertToSQL.New(t)
572+
}
573+
return val, nil
563574
}
564575

565576
// Compare implements Type interface.

0 commit comments

Comments
 (0)