Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Code ¶
type Code interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
// Message returns the error description string for the code.
Message() string
}
Code is a generic interface that represents an error code with an underlying integer type. It uses Go's type approximation (~int, ~int8, etc.) to allow any integer-based type. The Message method returns the error description associated with the code.
type Error ¶
type Error[C Code] interface { // Code returns typed code value Code() C // Error implements an error Error() string // Unwrap unwraps caused error Unwrap() error // Is returns if input is same type of CodeError and with same code Is(error) bool }
Error is a generic interface defining an error with a typed code. It extends the standard error interface with methods to access the typed code, unwrap the underlying cause, and compare errors by code. The generic parameter C must satisfy the Code interface.
Example ¶
fmt.Println(New(ECODE_UNDEFINED).Error())
e0 := Errorf(ECODE__REASON1, "user message: %d", 1)
fmt.Println(e0.Error())
e1 := Wrap(ECODE__REASON1, errors.New("cause1"))
fmt.Println(e1.Error())
fmt.Printf("cause by unwrapping: %v\n", errors.Unwrap(e1))
e2 := Wrapf(ECODE__REASON2, errors.New("cause2"), "user message: %s", "any")
fmt.Println(e2.Error())
fmt.Printf("cause by unwrapping: %v\n", errors.Unwrap(e2))
fmt.Printf("expecting true errors.Is(e0, e1): %v\n", errors.Is(e0, e1))
fmt.Printf("expecting false errors.Is(e1, e2): %v\n", errors.Is(e1, e2))
fmt.Printf("expecting nil Wrap(ECODE__REASON1, nil): %v\n", Wrap(ECODE__REASON1, nil))
fmt.Printf("expecting nil Wrapf(ECODE__REASON2, nil): %v\n", Wrapf(ECODE__REASON2, nil, "whatever"))
Output: [region:1] undefined [region:2] reason1. user message: 1 [region:2] reason1. [cause: cause1] cause by unwrapping: cause1 [region:3] reason2. user message: any. [cause: cause2] cause by unwrapping: cause2 expecting true errors.Is(e0, e1): true expecting false errors.Is(e1, e2): false expecting nil Wrap(ECODE__REASON1, nil): <nil> expecting nil Wrapf(ECODE__REASON2, nil): <nil>
Click to show internal directories.
Click to hide internal directories.