Skip to content

Commit

Permalink
fix: improve scan error message
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jan 28, 2022
1 parent 838ed6b commit 54048b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
38 changes: 29 additions & 9 deletions schema/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ func scanBool(dest reflect.Value, src interface{}) error {
}
dest.SetBool(f)
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanInt64(dest reflect.Value, src interface{}) error {
Expand Down Expand Up @@ -178,8 +179,9 @@ func scanInt64(dest reflect.Value, src interface{}) error {
}
dest.SetInt(n)
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanUint64(dest reflect.Value, src interface{}) error {
Expand Down Expand Up @@ -207,8 +209,9 @@ func scanUint64(dest reflect.Value, src interface{}) error {
}
dest.SetUint(n)
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanFloat64(dest reflect.Value, src interface{}) error {
Expand All @@ -233,8 +236,9 @@ func scanFloat64(dest reflect.Value, src interface{}) error {
}
dest.SetFloat(f)
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanString(dest reflect.Value, src interface{}) error {
Expand All @@ -251,8 +255,18 @@ func scanString(dest reflect.Value, src interface{}) error {
case time.Time:
dest.SetString(src.Format(time.RFC3339Nano))
return nil
case int64:
dest.SetString(strconv.FormatInt(src, 10))
return nil
case uint64:
dest.SetString(strconv.FormatUint(src, 10))
return nil
case float64:
dest.SetString(strconv.FormatFloat(src, 'G', -1, 64))
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanBytes(dest reflect.Value, src interface{}) error {
Expand All @@ -269,8 +283,9 @@ func scanBytes(dest reflect.Value, src interface{}) error {

dest.SetBytes(clone)
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanTime(dest reflect.Value, src interface{}) error {
Expand Down Expand Up @@ -299,8 +314,9 @@ func scanTime(dest reflect.Value, src interface{}) error {
destTime := dest.Addr().Interface().(*time.Time)
*destTime = srcTime
return nil
default:
return scanError(dest.Type(), src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}

func scanScanner(dest reflect.Value, src interface{}) error {
Expand Down Expand Up @@ -463,7 +479,7 @@ func scanJSONIntoInterface(dest reflect.Value, src interface{}) error {
if fn := Scanner(dest.Type()); fn != nil {
return fn(dest, src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
return scanError(dest.Type(), src)
}

func scanInterface(dest reflect.Value, src interface{}) error {
Expand All @@ -479,7 +495,7 @@ func scanInterface(dest reflect.Value, src interface{}) error {
if fn := Scanner(dest.Type()); fn != nil {
return fn(dest, src)
}
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
return scanError(dest.Type(), src)
}

func nilable(kind reflect.Kind) bool {
Expand All @@ -489,3 +505,7 @@ func nilable(kind reflect.Kind) bool {
}
return false
}

func scanError(dest reflect.Type, src interface{}) error {
return fmt.Errorf("bun: can't scan %#v (%T) into %s", src, src, dest.String())
}
3 changes: 1 addition & 2 deletions schema/sqltype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -135,6 +134,6 @@ func (tm *NullTime) Scan(src interface{}) error {
tm.Time = newtm
return nil
default:
return fmt.Errorf("bun: can't scan %#v into NullTime", src)
return scanError(bunNullTimeType, src)
}
}

0 comments on commit 54048b2

Please sign in to comment.