github.com/tursom/GoCollections@v0.3.10/lang/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 lang
     8  
     9  import "fmt"
    10  
    11  type TypeCastException struct {
    12  	RuntimeException
    13  }
    14  
    15  func NewTypeCastException(message string, err any, config *ExceptionConfig) *TypeCastException {
    16  	if message == "" {
    17  		message = "type cast failed"
    18  	}
    19  
    20  	return &TypeCastException{
    21  		RuntimeException: *NewRuntimeException(message, config.AddSkipStack(1).
    22  			SetCause(err).
    23  			SetExceptionName("github.com.tursom.GoCollections.exceptions.TypeCastException")),
    24  	}
    25  }
    26  
    27  func NewTypeCastException2[T any](v any, config *ExceptionConfig) *TypeCastException {
    28  	return NewTypeCastException(fmt.Sprintf("type %s cannot cast to %s", TypeNameOf(v), TypeName[T]()), nil, config)
    29  }