Help us shape the future of Go! Take the 2026 Go Developer Survey

errcode

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	URI_NOT_FOUND          = 1000
	SYSTEM_ERROR           = 1001
	DB_CONNECT_ERROR       = 1002
	REQUEST_PARAMETER_LOST = 1003
	DATA_NOT_FOUND         = 1004
	USER_NOT_FOUND         = 1005
	PASSWORD_ERROR         = 1006
	VERIFY_CODE_ERROR      = 1007
	TOKEN_ERROR            = 1008
	AUTHENTICATION_FAILURE = 1009
	SERVICE_UNAVAILABLE    = 1010
	PARAM_ERROR            = 1014 //参数校验错误
	TOO_MANY_REQUESTS      = 1011 //触发限流
	JOB_ERROR              = 1012 //定时任务执行异常
	STORAGE_ERROR          = 1013 //对象存储操作异常
)
View Source
const (
	ModuleCommon   = "common"
	ModuleAuth     = "auth"
	ModuleUser     = "user"
	ModuleDatabase = "database"
	ModuleJob      = "job"
	ModuleStorage  = "storage"
	ModuleClient   = "client"
	ModulePlugin   = "plugin"
)

Module 业务模块常量:用于错误码分组与文档归类。 v2 新增。v1 时代的错误码常量(URI_NOT_FOUND=1000 等)不重新分配模块,保持兼容。

View Source
const (
	UrlNotFound        = "404"
	SystemError        = "系统异常"
	DbConnectErr       = "数据库连接失败"
	DbInsertErr        = "数据库插入失败"
	DbUpdateErr        = "数据库更新失败"
	DbDeleteErr        = "数据库删除失败"
	DataNotFound       = "数据库查无数据"
	ParamLost          = "参数不可为空"
	ParamError         = "参数错误"
	ConnectFail        = "网络连接失败"
	ServiceUnavailable = "服务不存在"
	Success            = "success"
	DbQueryErr         = "数据库查询失败"
	TooManyRequests    = "请求过于频繁,请稍后再试"
	JobError           = "定时任务执行失败"
	StorageError       = "对象存储操作失败"
)

Variables

This section is empty.

Functions

func AllDefs

func AllDefs() map[int]Definition

AllDefs 导出全部已注册 Definition,主要给 docs / OpenAPI 生成器使用。

func RegisterDef

func RegisterDef(d Definition)

RegisterDef 把自定义 Definition 注册到全局映射(业务侧可在 init() 中调用)。 同 code 后注册会覆盖前者。

Types

type Definition

type Definition struct {
	Code       int
	HTTPStatus int
	MessageKey string
	Module     string
}

Definition v2 新增:一个标准化的"业务错误定义"。

字段含义:

  • Code: 业务错误码(int),与 v1 常量同空间(1000+,与 HTTP status 错开)
  • HTTPStatus: HTTP 状态码(int)。v1 时代所有错误都 200,v2 起按定义走正确状态码
  • MessageKey: i18n 翻译键(如 "errcode.uri_not_found"),用于 ErrorDef 自动取文案
  • Module: 业务模块,便于按模块聚合
  • Args: 可选,预留默认占位参数

func LookupDef

func LookupDef(code int) Definition

LookupDef 按 v1 错误码常量查 v2 Definition。未注册则返 500 + SYSTEM_ERROR。

func New

func New(module string, code, httpStatus int, messageKey string) Definition

New 构造一个 Definition。HTTPStatus 必须为 0 或一个合法 HTTP 状态码, 非法值会被自动修正为 500(服务端错误),避免误返 2xx 给客户端。

Jump to

Keyboard shortcuts

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