Documentation
¶
Index ¶
- Variables
- func CodeToMessage(code Code, msg string) string
- func CodeToMessageWithContext(ctx context.Context, code Code, msg string) string
- func InstallCode2Message(fnPre, fnEx FNCode2Message)
- func InstallCode2MessageEx(fnPre, fnEx FNCode2Message, ...)
- type Code
- type CodeError
- func CodeErrorFromError(err error, msg string) CodeError
- func FromError(err error) CodeError
- func FromErrorAndMessage(err error, msg string) CodeError
- func New(code Code) CodeError
- func NewEx(code Code, msg string) CodeError
- func TryGetCodeErrorFromError(err error) (CodeError, bool)
- func Wrap(code Code, cause error, msg ...string) CodeError
- func (e CodeError) Cause() error
- func (e CodeError) Code() Code
- func (e CodeError) Error() string
- func (e CodeError) GetCode() Code
- func (e CodeError) GetMsg() string
- func (e CodeError) Is(target error) bool
- func (e CodeError) Success() bool
- func (e CodeError) Unwrap() error
- func (e CodeError) WithCause(cause error) CodeError
- func (e CodeError) WithMsg(msg string) CodeError
- type FNCode2Message
- type FNCode2MessageWithContext
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) ErrTimeout = New(CodeErrTimeout) )
--- Global sentinel errors (recommended for errors.Is) ---
Functions ¶
func CodeToMessage ¶
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 CodeErrTimeout CodeErrCustomStart Code = 1000 CodeErrCustomEnd Code = 8000 )
func CodeFromError ¶
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)
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 FromError ¶
FromError converts any error into a CodeError.
- nil → success
- CodeError in chain → returns it
- otherwise → CodeErrInternal with original error as cause
func FromErrorAndMessage ¶
func (CodeError) Error ¶
Error returns the error message. Uses custom msg if set; otherwise falls back to code.String().
func (CodeError) Is ¶
Is reports whether the current error matches the target by code. Uses errors.As to walk the entire error chain.
type FNCode2Message ¶
Click to show internal directories.
Click to hide internal directories.