codex

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 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 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
package main

import (
	"errors"
	"fmt"

	. "github.com/xoctopus/x/codex"
)

type ECode int8

const (
	ECODE_UNDEFINED ECode = iota + 1
	ECODE__REASON1        // reason1
	ECODE__REASON2        // reason2
)

func (e ECode) Message() string {
	prefix := fmt.Sprintf("[region:%d] ", e)

	switch e {
	case ECODE_UNDEFINED:
		return prefix + "undefined"
	case ECODE__REASON1:
		return prefix + "reason1"
	case ECODE__REASON2:
		return prefix + "reason2"
	default:
		return prefix + "unknown"
	}
}

func main() {
	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>

Jump to

Keyboard shortcuts

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