GoCollections/exceptions/NPE.go

32 lines
654 B
Go
Raw Normal View History

2022-11-25 18:19:33 +08:00
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
2021-05-21 11:14:07 +08:00
package exceptions
2022-03-28 18:19:05 +08:00
import (
"reflect"
2022-11-25 18:19:33 +08:00
"github.com/tursom/GoCollections/lang"
2022-03-28 18:19:05 +08:00
)
2021-05-21 11:14:07 +08:00
type NPE struct {
RuntimeException
}
2022-03-31 01:22:30 +08:00
func NewNPE(message string, config *ExceptionConfig) *NPE {
2022-03-21 11:02:41 +08:00
return &NPE{
*NewRuntimeException(message, config.AddSkipStack(1).
2022-04-02 15:25:35 +08:00
SetExceptionName("github.com.tursom.GoCollections.exceptions.NPE")),
2021-05-21 11:14:07 +08:00
}
}
2022-03-28 18:19:05 +08:00
func CheckNil[T any](p *T) {
t := reflect.TypeOf(lang.Nil[T]())
if p == nil {
panic(NewNPE(t.Name()+" is null", DefaultExceptionConfig().AddSkipStack(1)))
}
}