github.com/tursom/GoCollections@v0.3.10/exceptions/TypeCastException.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package exceptions
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  
    13  	"github.com/tursom/GoCollections/lang"
    14  )
    15  
    16  type TypeCastException = lang.TypeCastException
    17  
    18  func NewTypeCastException(message string, config *ExceptionConfig) *TypeCastException {
    19  	return &TypeCastException{
    20  		*NewRuntimeException(message, config.AddSkipStack(1).
    21  			SetExceptionName("github.com.tursom.GoCollections.exceptions.TypeCastException")),
    22  	}
    23  }
    24  
    25  func NewTypeCastExceptionByType[T any](obj any, config *ExceptionConfig) *TypeCastException {
    26  	return NewTypeCastException(
    27  		fmt.Sprintf("object %s cannot cast to %s", obj, reflect.TypeOf(lang.Nil[T]()).Name()),
    28  		config,
    29  	)
    30  }