core/cerror/code.go

37 lines
521 B
Go
Raw Permalink Normal View History

2023-05-19 04:12:21 +00:00
package cerror
import (
"git.twelvetwelve.org/library/core/log"
)
var codes = make(map[string]*Code)
type Code struct {
code string
}
func (c *Code) Error() string {
return c.code
}
func NewCode(urn string) *Code {
if _, ok := codes[urn]; ok {
log.Panicf("error code %s already exists", urn)
}
code := &Code{
code: urn,
}
codes[urn] = code
return code
}
func GetCode(urn string) *Code {
if code, ok := codes[urn]; ok {
return code
}
log.Panicf("error code %s does not exist", urn)
return nil
}