GoCollections/exceptions/TypeCastException.go

25 lines
587 B
Go
Raw Normal View History

2022-03-28 18:19:05 +08:00
package exceptions
import (
"fmt"
"github.com/tursom/GoCollections/lang"
"reflect"
)
type TypeCastException struct {
RuntimeException
}
2022-03-31 01:22:30 +08:00
func NewTypeCastException(message string, config *ExceptionConfig) *TypeCastException {
2022-03-28 18:19:05 +08:00
return &TypeCastException{
2022-03-31 01:22:30 +08:00
NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("TypeCastException")),
2022-03-28 18:19:05 +08:00
}
}
func NewTypeCastExceptionByType[T any](obj any, config *ExceptionConfig) *TypeCastException {
return NewTypeCastException(
fmt.Sprintf("object %s cannot cast to %s", obj, reflect.TypeOf(lang.Nil[T]()).Name()),
config,
)
}