errorx

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSuccess           = New(CodeSuccess)
	ErrUnknown           = New(CodeErrUnknown)
	ErrCommunication     = New(CodeErrCommunication) // ← ADD THIS
	ErrInvalidArgs       = New(CodeErrInvalidArgs)
	ErrInternal          = New(CodeErrInternal)
	ErrFail              = New(CodeErrFail)
	ErrNoMoreData        = New(CodeErrNoMoreData)
	ErrSkip              = New(CodeErrSkip)
	NoErrSkip            = New(CodeSkip)
	ErrBadToken          = New(CodeErrBadToken)
	ErrInvalidToken      = New(CodeErrInvalidToken)
	ErrNeedAuth          = New(CodeErrNeedAuth)
	ErrVerify            = New(CodeErrVerify)
	ErrExists            = New(CodeErrExists)
	ErrNotExists         = New(CodeErrNotExists)
	ErrDisabled          = New(CodeErrDisabled)
	ErrConflict          = New(CodeErrConflict)
	ErrLogic             = New(CodeErrLogic)
	ErrResourceExhausted = New(CodeErrResourceExhausted)
	ErrPartSuccess       = New(CodeErrPartSuccess)
	ErrUnimplemented     = New(CodeErrUnimplemented)
	ErrCrashed           = New(CodeErrCrashed)
	ErrOverflow          = New(CodeErrOverflow)
)

--- Global sentinel errors (recommended for errors.Is) ---

Functions

func CodeToMessage

func CodeToMessage(code Code, msg string) string

func CodeToMessageWithContext

func CodeToMessageWithContext(ctx context.Context, code Code, msg string) string

func InstallCode2Message

func InstallCode2Message(fnPre, fnEx FNCode2Message)

InstallCode2Message warning, not thread safe

func InstallCode2MessageEx

func InstallCode2MessageEx(fnPre, fnEx FNCode2Message, fnPreWithContext, fnExWithContext FNCode2MessageWithContext)

InstallCode2MessageEx warning, not thread safe

Types

type Code

type Code int

Code represents a business-level error code. All codes are positive integers. Zero is reserved for success.

const (
	CodeSuccess Code = iota

	CodeErrUnknown  // Unknown error (catch-all)
	CodeErrInternal // Server internal error
	CodeErrFail     // General operation failure (use when no better code fits)

	CodeErrNoMoreData
	CodeErrSkip
	CodeSkip

	CodeErrCommunication // Network / RPC / timeout / connection issues

	CodeErrInvalidArgs // Invalid or malformed arguments

	CodeErrBadToken     // Token is malformed
	CodeErrInvalidToken // Token is valid but expired / revoked
	CodeErrNeedAuth     // Authentication required
	CodeErrVerify       // Verification failed (e.g. CAPTCHA, email)

	CodeErrExists    // Resource already exists
	CodeErrNotExists // Resource does not exist
	CodeErrDisabled  // Resource is disabled
	CodeErrConflict  // State conflict (e.g. concurrent update)

	CodeErrLogic // Business rule violation

	CodeErrResourceExhausted // Rate limit, quota, storage full, etc.

	CodeErrPartSuccess // Operation partially succeeded

	CodeErrUnimplemented // Feature not implemented

	CodeErrCrashed
	CodeErrOverflow

	CodeErrCustomStart Code = 1000
	CodeErrCustomEnd   Code = 8000
)

func CodeFromError

func CodeFromError(err error) (code Code, errorMsg string)

CodeFromError extracts the business code and message from any error.

  • nil → (CodeSuccess, "")
  • CodeError found → (code, custom_msg or code.String())
  • otherwise → (CodeErrInternal, original_error_string)

func (Code) Key

func (c Code) Key() string

func (Code) String

func (c Code) String() string

func (Code) StringWithContext

func (c Code) StringWithContext(ctx context.Context) string

type CodeError

type CodeError struct {
	// contains filtered or unexported fields
}

CodeError is a structured error carrying:

  • a business code
  • an optional custom message
  • an optional underlying cause (for error chaining)

func CodeErrorFromError

func CodeErrorFromError(err error, msg string) CodeError

func FromError

func FromError(err error) CodeError

FromError converts any error into a CodeError.

  • nil → success
  • CodeError in chain → returns it
  • otherwise → CodeErrInternal with original error as cause

func FromErrorAndMessage

func FromErrorAndMessage(err error, msg string) CodeError

func New

func New(code Code) CodeError

New creates a CodeError with the given code and default message.

func NewEx

func NewEx(code Code, msg string) CodeError

NewEx creates a CodeError with a custom message.

func TryGetCodeErrorFromError

func TryGetCodeErrorFromError(err error) (CodeError, bool)

func Wrap

func Wrap(code Code, cause error, msg ...string) CodeError

Wrap wraps an underlying error with a business code and optional message.

func (CodeError) Cause

func (e CodeError) Cause() error

Cause returns the underlying error (for inspection).

func (CodeError) Code

func (e CodeError) Code() Code

Code returns the error code (enables type assertion).

func (CodeError) Error

func (e CodeError) Error() string

Error returns the error message. Uses custom msg if set; otherwise falls back to code.String().

func (CodeError) GetCode

func (e CodeError) GetCode() Code

GetCode returns the error code.

func (CodeError) GetMsg

func (e CodeError) GetMsg() string

GetMsg returns the custom message (empty if not set).

func (CodeError) Is

func (e CodeError) Is(target error) bool

Is reports whether the current error matches the target by code. Uses errors.As to walk the entire error chain.

func (CodeError) Success

func (e CodeError) Success() bool

Success reports whether the operation succeeded.

func (CodeError) Unwrap

func (e CodeError) Unwrap() error

Unwrap returns the wrapped error to support errors.Unwrap and errors.Is.

func (CodeError) WithCause

func (e CodeError) WithCause(cause error) CodeError

WithCause returns a copy with the given underlying error.

func (CodeError) WithMsg

func (e CodeError) WithMsg(msg string) CodeError

WithMsg returns a copy with the given custom message.

type FNCode2Message

type FNCode2Message func(code Code) (msg string, ok bool)

type FNCode2MessageWithContext

type FNCode2MessageWithContext func(ctx context.Context, code Code) (msg string, ok bool)

Jump to

Keyboard shortcuts

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