代码拉取完成,页面将自动刷新
// Package errors provides a way to return detailed information
// for an RPC request error. The error is normally JSON encoded.
package errors
import (
"fmt"
json "github.com/json-iterator/go"
)
//go:generate protoc -I. --go_out=paths=source_relative:. errors.proto
const (
Silent = "0"
MessageWarn = "1"
MessageError = "2"
Notification = "4"
Page = "9"
)
func (e *Error) Error() string {
b, _ := json.Marshal(e)
return string(b)
}
// New generates a custom error.
func New(id, domain string, code ErrorCoder) error {
return &Error{
ErrorCode: fmt.Sprintf("C%d", code.Code()),
ErrorMessage: code.String(),
ShowType: MessageError,
TraceId: id,
Domain: domain,
}
}
// Parse tries to parse a JSON string into an error. If that
// fails, it will set the given string as the error detail.
func Parse(errStr string) *Error {
e := new(Error)
err := json.Unmarshal([]byte(errStr), e)
if err != nil {
e.ErrorMessage = errStr
}
return e
}
// Equal tries to compare errors
func Equal(err1 error, err2 error) bool {
verr1, ok1 := err1.(*Error)
verr2, ok2 := err2.(*Error)
if ok1 != ok2 {
return false
}
if !ok1 {
return err1 == err2
}
if verr1.ErrorCode != verr2.ErrorCode {
return false
}
return true
}
// FromError try to convert go error to *Error
func FromError(err error) *Error {
if verr, ok := err.(*Error); ok && verr != nil {
return verr
}
return Parse(err.Error())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。