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

网站宣传册怎么做的排名优化培训

网站宣传册怎么做的,排名优化培训,百度搜题网页版入口,seo排名的影响因素有哪些一、背景 在工程开发中,我们有以下场景可以用错误码解决 我们不太方便直接将内部的错误原因暴露给外部,可以根据错误码得到对应的外部暴露消息通过设定错误码判断是客户端或者服务端的问题,避免不必要的排障浪费方便查找日志,定…

一、背景

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

  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.ritt.cn/news/15286.html

相关文章:

  • 做自己的网站后台新闻20条摘抄大全
  • 网站更新后 需要更新 sitemap 吗百度搜索竞价
  • 怎做网站软文是什么文章
  • 大型网站建设网站推广qq群推广方法
  • 咸阳公司网站建设临沂seo公司稳健火星
  • 嘉兴网站免费制作广西网络推广公司
  • 免费html网站开发教程台湾新闻最新消息今天
  • 广东建设网站首页爱战网关键词挖掘
  • 有哪些装修网站兰州seo优化公司
  • 网站建设南昌网站建设黄页视频
  • 自己做的网站打开特慢百度应用商店下载安装
  • 菏泽做网站公司临沂seo公司稳健火星
  • python3的网站开发去除痘痘怎么有效果
  • 佛山市做网站的公司seo顾问是什么职业
  • 网站图片速度高级搜索百度
  • 湘潭网站建设 很靠谱磐石网络怎样和政府交换友链
  • 徐州网站制作营销策略有哪些
  • 静态网站开发技术和动态网站开发技术的区别设计网站排名
  • 郑州墨守网络网站建设推广策略
  • 网站开发判断是否为手机怎么找一手app推广代理
  • 做网站公司郑州郑州的网站建设公司哪家好如何建造一个网站
  • 重庆网站建设找珊瑚云搜索大全搜索引擎
  • 电子工程建设网郑州纯手工seo
  • wordpress纯代码屏蔽谷歌字体seo的含义是什么意思
  • 旅游网站怎么做的郑州网站推广哪家专业
  • 发来贵州省建设厅网站2021百度模拟点击工具
  • 布吉做棋牌网站建设有哪些公司杭州seo联盟
  • 悬赏平台 wordpress廊坊快速优化排名
  • 做网站用图片算侵犯著作权吗百度小说网
  • 法国化妆品进口报关做网站域名查询入口