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

Commit 499cba7

Browse files
author
kuba--
committed
Make conversion error clearer and less detailed.
Signed-off-by: kuba-- <[email protected]>
1 parent 5a108b2 commit 499cba7

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.
@@ -300,7 +304,6 @@ func (t numberT) Convert(v interface{}) (interface{}, error) {
300304
default:
301305
return nil, ErrInvalidType.New(t.t)
302306
}
303-
304307
}
305308

306309
// Compare implements Type interface.
@@ -505,7 +508,11 @@ func (t textT) SQL(v interface{}) sqltypes.Value {
505508

506509
// Convert implements Type interface.
507510
func (t textT) Convert(v interface{}) (interface{}, error) {
508-
return cast.ToStringE(v)
511+
val, err := cast.ToStringE(v)
512+
if err != nil {
513+
return nil, ErrConvertToSQL.New(t)
514+
}
515+
return val, nil
509516
}
510517

511518
// Compare implements Type interface.
@@ -534,7 +541,11 @@ func (t booleanT) SQL(v interface{}) sqltypes.Value {
534541

535542
// Convert implements Type interface.
536543
func (t booleanT) Convert(v interface{}) (interface{}, error) {
537-
return cast.ToBoolE(v)
544+
val, err := cast.ToBoolE(v)
545+
if err != nil {
546+
return nil, ErrConvertToSQL.New(t)
547+
}
548+
return val, nil
538549
}
539550

540551
// Compare implements Type interface.

0 commit comments

Comments
 (0)