网站宣传册怎么做的排名优化培训
一、背景
在工程开发中,我们有以下场景可以用错误码解决
- 我们不太方便直接将内部的错误原因暴露给外部,可以根据错误码得到对应的外部暴露消息
- 通过设定错误码判断是客户端或者服务端的问题,避免不必要的排障浪费
- 方便查找日志,定位问题
错误码设计
设计带有错误码的error
核心代码
type withCode struct {msg stringcode int
}func WithCode(code int, format string, args ...interface{}) error {msg := fmt.Sprintf(format, args...)return errors.Wrap(&withCode{msg: msg,code: code,}, msg)
}// Error return the externally-safe error message.
func (w *withCode) Error() string { return msg }func ParseCoder(err error) Coder {codeErr := &withCoder{}if errors.As(err, codeErr) {if coder, ok := codes[codeErr.code]; ok {return coder}}return UnknownCoder
}
完整代码
github.com/marmotedu/errors v1.0.2
Coder的设计
提供register方法将错误码和相关信息注册在map内部,需要用到错误码信息的时候根据code拿到相关信息返回即可
接口设计
type Coder interface {// HTTP status that should be used for the associated error code.HTTPStatus() int// External (user) facing error text.String() string// Code returns the code of the coderCode() int
}
完整实现
var codes = map[int]Coder{}
var codeMux = &sync.Mutex{}type Coder interface {// HTTP status that should be used for the associated error code.HTTPStatus() int// External (user) facing error text.String() string// Code returns the code of the coderCode() int
}type ErrCode struct {// C refers to the code of the ErrCode.C int// HTTP status that should be used for the associated error code.HTTP int// External (user) facing error text.Ext string
}var _ errors.Coder = &ErrCode{}// Code returns the integer code of ErrCode.
func (coder ErrCode) Code() int {return coder.C
}// String implements stringer. String returns the external error message,
// if any.
func (coder ErrCode) String() string {return coder.Ext
}// HTTPStatus returns the associated HTTP status code, if any. Otherwise,
// returns 200.
func (coder ErrCode) HTTPStatus() int {if coder.HTTP == 0 {return http.StatusInternalServerError}return coder.HTTP
}// MustRegister register a user define error code.
// It will panic when the same Code already exist.
func MustRegister(coder Coder) {if coder.Code() == 0 {panic("code '0' is reserved by 'github.com/marmotedu/errors' as ErrUnknown error code")}codeMux.Lock()defer codeMux.Unlock()if _, ok := codes[coder.Code()]; ok {panic(fmt.Sprintf("code: %d already exist", coder.Code()))}codes[coder.Code()] = coder
}// ParseCoder parse any error into *withCode.
// nil error will return nil direct.
// None withStack error will be parsed as ErrUnknown.
func ParseCoder(err error) Coder {if err == nil {return nil}if v, ok := err.(*withCode); ok {if coder, ok := codes[v.code]; ok {return coder}}return unknownCoder
}// IsCode reports whether any error in err's chain contains the given error code.
func IsCode(err error, code int) bool {if v, ok := err.(*withCode); ok {if v.code == code {return true}if v.cause != nil {return IsCode(v.cause, code)}return false}return false
}func init() {codes[unknownCoder.Code()] = unknownCoder
}
错误码注册
在工程内部创建一个文件专门设置错误码,并实现init方法针对错误码和信息完成注册
const (// ErrUserNotFound - 404: User not found.ErrUserNotFound int = iota + 110001// ErrUserAlreadyExist - 400: User already exist.ErrUserAlreadyExist
)// iam-apiserver: secret errors.
const (// ErrEncrypt - 400: Secret reach the max count.ErrReachMaxCount int = iota + 110101// ErrSecretNotFound - 404: Secret not found.ErrSecretNotFound
)// iam-apiserver: policy errors.
const (// ErrPolicyNotFound - 404: Policy not found.ErrPolicyNotFound int = iota + 110201
)func init() {register(ErrUserNotFound, 404, "User not found")register(ErrUserAlreadyExist, 400, "User already exist")register(ErrReachMaxCount, 400, "Secret reach the max count")register(ErrSecretNotFound, 404, "Secret not found")register(ErrPolicyNotFound, 404, "Policy not found")register(ErrSuccess, 200, "OK")register(ErrUnknown, 500, "Internal server error")register(ErrBind, 400, "Error occurred while binding the request body to the struct")register(ErrValidation, 400, "Validation failed")
}
错误码使用
func WriteResponse(c *gin.Context, err error, data interface{}) {if err == nil {c.JSON(http.statusOK, data)return}if coder := coder.ParseCoder(err); coder != coder.UnknownCoder {c.JSON(coder.HTTPStatus, Response{Code: coder.Code(),Message: coeder.String(),Data: data, })return }c.JSON(http.StatusOk, err)