goerror

package
v1.2.88 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: Apache-2.0 Imports: 3 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 FORBIDDEN_ERROR uint32 = 403
View Source
const NOT_FOUND_ERROR uint32 = 404
View Source
const OK uint32 = 0

成功返回

View Source
const REQUEST_PARAM_ERROR uint32 = 400

全局错误码 client

View Source
const SERVER_COMMON_ERROR uint32 = 500

server

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)
)

Functions

func GetErrCodeMsg

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

func Init added in v1.2.87

func Init(errs map[uint32]string)

扩展的错误类型这里初始化

func IsCodeErr

func IsCodeErr(errcode uint32) bool

func IsErrCode

func IsErrCode(err error, code uint32) bool

func MapErrMsg

func MapErrMsg(errcode uint32) string

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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