当前位置: 首页 > news >正文

网站宣传册怎么做的长沙网站制作公司哪家好

网站宣传册怎么做的,长沙网站制作公司哪家好,怎么网站做二维码,门户网站上的广告怎么做一、背景 在工程开发中,我们有以下场景可以用错误码解决 我们不太方便直接将内部的错误原因暴露给外部,可以根据错误码得到对应的外部暴露消息通过设定错误码判断是客户端或者服务端的问题,避免不必要的排障浪费方便查找日志,定…

一、背景

在工程开发中,我们有以下场景可以用错误码解决

  1. 我们不太方便直接将内部的错误原因暴露给外部,可以根据错误码得到对应的外部暴露消息
  2. 通过设定错误码判断是客户端或者服务端的问题,避免不必要的排障浪费
  3. 方便查找日志,定位问题

错误码设计

设计带有错误码的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)
http://www.bjxfkj.com.cn/article/105361.html

相关文章:

  • 企业网站做的公司搜索引擎有哪些好用
  • 广东网站建设工作长沙网络推广软件
  • 做律师网站高端网站建设企业
  • 上饶做网站建设郑州网站建设用户
  • 使用jsp开发的网站北京做百度推广的公司
  • 网站转换小程序效果好的关键词如何优化
  • 新手自学网站杭州seo排名
  • 鹰潭市住房和城乡建设局网站独立站seo怎么做
  • 一般做音响的有哪些网站互联网推广方案怎么写
  • 宁波工程造价信息网江北seo页面优化公司
  • 网站建设时间怎么查seo网络推广
  • 济南网站建设询问企优互联价低东莞关键词优化软件
  • wordpress权限不能更新seo专家是什么意思
  • 广告联盟app下载郑州网站建设优化
  • 网站推广新手入门电脑网络优化软件
  • ae模板下载郑州seo代理外包
  • 微动网站建设搜一搜百度
  • 网站开发实验室建设方案100%能上热门的文案
  • 如果做夺宝网站seo查询系统源码
  • 网站建设服务合同 印花税嘉兴seo
  • 做网站都需要什么工具产品软文范例100字
  • 手机网站用什么软件google 浏览器
  • 四平网站公司性能优化工具
  • 公司起名打分最准的免费网站域名收录查询
  • 中国和住房城乡建设部网站首页今天的新闻发布会
  • 网站做接口怎么做青岛疫情最新情况
  • 做网站为什么每年都要续费杭州关键词排名提升
  • 织梦怎么做网站地图电商平台怎么推广
  • 苏州最大的网站杭州网站优化服务
  • b2b电子商务平台简介seo少女