42 lines
670 B
Go
42 lines
670 B
Go
|
package reflect
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type ErrCanNotConvertType struct {
|
||
|
value interface{}
|
||
|
from reflect.Type
|
||
|
to reflect.Type
|
||
|
reason []string
|
||
|
}
|
||
|
|
||
|
func (e ErrCanNotConvertType) Error() string {
|
||
|
var (
|
||
|
reason = strings.Join(e.reason, ", ")
|
||
|
)
|
||
|
|
||
|
if reason != "" {
|
||
|
reason = ", reason: " + reason
|
||
|
}
|
||
|
|
||
|
return fmt.Sprintf(
|
||
|
"Can not convert '%#v' of type '%s' to '%s'%s",
|
||
|
e.value,
|
||
|
e.from,
|
||
|
e.to,
|
||
|
reason,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func NewErrCanNotConvertType(value interface{}, from reflect.Type, to reflect.Type, reason ...string) ErrCanNotConvertType {
|
||
|
return ErrCanNotConvertType{
|
||
|
value: value,
|
||
|
from: from,
|
||
|
to: to,
|
||
|
reason: reason,
|
||
|
}
|
||
|
}
|