goerror

package
v1.2.181 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

有状态信息的错误封装

package main

import (
	goerror "github.com/gif-gif/go.io/go-error"
	golog "github.com/gif-gif/go.io/go-log"
	"github.com/pkg/errors"
)

func main() {
	err := errors.Wrapf(goerror.NewErrCode(goerror.DB_ERROR), "find customer db err, in:%v , err:%v", "args", "err")
	if goerror.IsCodeErr(goerror.DB_ERROR) {
		golog.WithTag("goerror1").Error(err.Error(), " db error")
	}

	if goerror.IsErrCode(err, goerror.DB_ERROR) {
		golog.WithTag("goerror2").Error(err.Error(), " db error")
	}
	errCode, errMsg := goerror.GetErrCodeMsg(err)
	golog.WithTag("goerror3").Error(errCode, errMsg)
}

Documentation

Index

Constants

View Source
const CAPTCHA_ERROR uint32 = 700
View Source
const DB_ERROR uint32 = 555
View Source
const ETCD_ERROR uint32 = 777
View Source
const FORBIDDEN_ERROR uint32 = 403
View Source
const NOT_FOUND_ERROR uint32 = 404
View Source
const OK uint32 = 0

常用错误码 成功返回

View Source
const REDIS_ERROR uint32 = 666
View Source
const REQUEST_PARAM_ERROR uint32 = 400

全局错误码 client

View Source
const SERVER_COMMON_ERROR uint32 = 500

server

View Source
const ShowTypeMessageError uint32 = 2
View Source
const ShowTypeMessageWarn uint32 = 1
View Source
const ShowTypeNotification uint32 = 4
View Source
const ShowTypePage uint32 = 9
View Source
const ShowTypeSilent uint32 = 0

错误处理枚举 showType?: number; // error display type: 0 silent; 1 message.warn; 2 message.error; 4 notification; 9 page

View Source
const TOKEN_EXPIRE_ERROR uint32 = 401
View Source
const USER_EXISTS_ERROR uint32 = 704
View Source
const USER_LOGIN_ERROR uint32 = 703
View Source
const USER_NOT_EXISTS_ERROR uint32 = 701

Variables

View Source
var (
	ErrNotFound          = NewErrCodeMsg(NOT_FOUND_ERROR, "not found")
	ErrUserNoExists      = NewErrCodeMsg(USER_NOT_EXISTS_ERROR, "user not found")
	ErrUserExists        = NewErrCodeMsg(USER_EXISTS_ERROR, "user exists")
	ErrUserForbidden     = NewErrCode(FORBIDDEN_ERROR)
	ErrUserLogin         = NewErrCode(USER_LOGIN_ERROR)
	ErrUnauthorized      = NewErrCodeMsg(TOKEN_EXPIRE_ERROR, "unauthorized")
	ErrCaptcha           = NewErrCodeMsg(CAPTCHA_ERROR, "captcha error")
	ErrRequestParamError = NewErrCode(REQUEST_PARAM_ERROR)
	ErrDBError           = NewErrCode(DB_ERROR)
	ErrServerError       = NewErrCode(SERVER_COMMON_ERROR)
	ErrRedisError        = NewErrCode(REDIS_ERROR)
	ErrEtcdError         = NewErrCode(ETCD_ERROR)
)

Functions

func GetErrCodeMsg

func GetErrCodeMsg(err error) (errCode uint32, errMsg string)

建议用 GetCodeError

func GetStack added in v1.2.147

func GetStack() string

func Init added in v1.2.87

func Init(errs map[uint32]string)

扩展的错误类型这里初始化, GetErrCodeMsg 中自动处理 优先使用自定义错误类型,然后再使用扩展错误类型

func IsCodeErr

func IsCodeErr(errcode uint32) bool

func IsDatabaseNoRowsError added in v1.2.148

func IsDatabaseNoRowsError(err error) bool

func IsErrCode

func IsErrCode(err error, code uint32) bool

func MapErrMsg

func MapErrMsg(errcode uint32) string

func NewError added in v1.2.98

func NewError(errCode uint32) error

func NewErrorMsg added in v1.2.98

func NewErrorMsg(errCode uint32, errMsg string) error

Types

type CodeError

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

func NewErrCode

func NewErrCode(errCode uint32) *CodeError

func NewErrCodeMsg

func NewErrCodeMsg(errCode uint32, errMsg string) *CodeError

func NewErrMsg

func NewErrMsg(errMsg string) *CodeError

func NewParamErrMsg

func NewParamErrMsg(errMsg string) *CodeError

func (*CodeError) Error

func (e *CodeError) Error() string

func (*CodeError) GetErrCode

func (e *CodeError) GetErrCode() uint32

返回给前端的错误码

func (*CodeError) GetErrMsg

func (e *CodeError) GetErrMsg() string

返回给前端显示端错误信息

func (*CodeError) GetHost added in v1.2.177

func (e *CodeError) GetHost() string

func (*CodeError) GetShowType added in v1.2.177

func (e *CodeError) GetShowType() uint32

func (*CodeError) GetTraceId added in v1.2.177

func (e *CodeError) GetTraceId() string

type CodeErrorBuilder added in v1.2.177

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

CodeErrorBuilder 是 CodeError 的构建器

func GetCodeError added in v1.2.177

func GetCodeError(err error) *CodeErrorBuilder

CodeErrorBuilder.build() 构建 CodeError

返回错误码 CodeErrorBuilder

func NewCodeErrorBuilder added in v1.2.177

func NewCodeErrorBuilder() *CodeErrorBuilder

NewCodeErrorBuilder 创建一个新的 CodeErrorBuilder

func (*CodeErrorBuilder) Build added in v1.2.177

func (b *CodeErrorBuilder) Build() *CodeError

Build 构建并返回 CodeError 实例

func (*CodeErrorBuilder) WithErrCode added in v1.2.177

func (b *CodeErrorBuilder) WithErrCode(errCode uint32) *CodeErrorBuilder

WithErrCode 设置错误码

func (*CodeErrorBuilder) WithErrMsg added in v1.2.177

func (b *CodeErrorBuilder) WithErrMsg(errMsg string) *CodeErrorBuilder

WithErrMsg 设置错误信息

func (*CodeErrorBuilder) WithHost added in v1.2.177

func (b *CodeErrorBuilder) WithHost(host string) *CodeErrorBuilder

WithHost 设置主机信息

func (*CodeErrorBuilder) WithShowType added in v1.2.177

func (b *CodeErrorBuilder) WithShowType(showType uint32) *CodeErrorBuilder

WithShowType 设置显示类型

func (*CodeErrorBuilder) WithTraceId added in v1.2.177

func (b *CodeErrorBuilder) WithTraceId(traceId string) *CodeErrorBuilder

WithTraceId 设置追踪ID

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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