codex

package
v0.2.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 2 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorf

func Errorf[C Code](e C, format string, args ...any) error

func Is added in v0.2.7

func Is[C Code](e error) (C, bool)

func IsCode added in v0.2.7

func IsCode[C Code](e error, code C) bool

func New

func New[C Code](e C) error

func Wrap

func Wrap[C Code](e C, cause error) error

func Wrapf

func Wrapf[C Code](e C, cause error, format string, args ...any) error

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>

func As added in v0.2.7

func As[C Code](e error) (Error[C], bool)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL