errors

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

errors

errors 提供统一错误码、错误消息和错误包装能力。

导入

import foundationErrors "github.com/jackman0925/go-foundation/errors"

基础用法

err := foundationErrors.New(foundationErrors.CodeBadRequest, "参数错误")
err = foundationErrors.Wrap(foundationErrors.CodeInternal, "查询失败", err)

注意事项

  • 本包只放通用错误码;
  • 具体业务错误码应在业务项目中定义;
  • Wrap 会保留原始 error,支持标准库 errors.Is 和 errors.As。

Documentation

Index

Constants

View Source
const (
	// CodeOK 表示操作成功。
	CodeOK = 0
	// CodeBadRequest 表示请求参数无效。
	CodeBadRequest = 40000
	// CodeUnauthorized 表示缺少认证或认证无效。
	CodeUnauthorized = 40100
	// CodeForbidden 表示操作被禁止。
	CodeForbidden = 40300
	// CodeNotFound 表示资源不存在。
	CodeNotFound = 40400
	// CodeInternal 表示内部服务错误。
	CodeInternal = 50000
)

Variables

This section is empty.

Functions

func CodeOf

func CodeOf(err error) int

CodeOf 返回 AppError 错误码;未知错误返回 CodeInternal。

func MessageOf

func MessageOf(err error) string

MessageOf 返回 AppError 错误信息;未知错误返回 error 字符串。

func New

func New(code int, message string) error

New 创建不包含原始错误的 AppError。

func Wrap

func Wrap(code int, message string, cause error) error

Wrap 创建 AppError 并保留原始错误。

Types

type AppError

type AppError struct {
	Code    int
	Message string
	// contains filtered or unexported fields
}

AppError 携带稳定错误码和公开错误信息,并可包装原始错误。

func (*AppError) Error

func (e *AppError) Error() string

Error 返回公开错误信息。

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap 返回原始错误,用于 errors.Is 和 errors.As。

Jump to

Keyboard shortcuts

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