xerror

package
v0.0.0-...-0b6f334 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 13 Imported by: 2

Documentation

Index

Constants

View Source
const (
	BizNote      = 20000
	BizPassport  = 30000
	BizComment   = 40000
	BizCounter   = 50000
	BizMsger     = 60000
	BizRelation  = 70000
	BizWsLink    = 80000
	BizConductor = 90000
)
View Source
const (
	CodeOk                 = 0
	CodeUnauthorized       = 10001
	CodeInvalidParam       = 10002
	CodeForbidden          = 10003
	CodeNotFound           = 10004
	CodeInternal           = 10005
	CodeCsrfFailed         = 10006
	CodeServiceUnavailable = 10007
	CodeDuplicate          = 10008
	CodeTooLong            = 10009
	CodeServerSigningFail  = 10010
	CodeResourceNotFound   = 10011
	CodeInternalPanic      = 19000
	CodeOther              = 19999
)

业务错误码定义

View Source
const (
	Continue           = 100
	SwitchingProtocols = 101
	Processing         = 102
	EarlyHints         = 103

	OK                   = 200
	Created              = 201
	Accepted             = 202
	NonAuthoritativeInfo = 203
	NoContent            = 204
	ResetContent         = 205
	PartialContent       = 206
	MultiStatus          = 207
	AlreadyReported      = 208
	IMUsed               = 226

	MultipleChoices  = 300
	MovedPermanently = 301
	Found            = 302
	SeeOther         = 303
	NotModified      = 304
	UseProxy         = 305

	TemporaryRedirect = 307
	PermanentRedirect = 308

	BadRequest                   = 400
	Unauthorized                 = 401
	PaymentRequired              = 402
	Forbidden                    = 403
	NotFound                     = 404
	MethodNotAllowed             = 405
	NotAcceptable                = 406
	ProxyAuthRequired            = 407
	RequestTimeout               = 408
	Conflict                     = 409
	Gone                         = 410
	LengthRequired               = 411
	PreconditionFailed           = 412
	RequestEntityTooLarge        = 413
	RequestURITooLong            = 414
	UnsupportedMediaType         = 415
	RequestedRangeNotSatisfiable = 416
	ExpectationFailed            = 417
	Teapot                       = 418
	MisdirectedRequest           = 421
	UnprocessableEntity          = 422
	Locked                       = 423
	FailedDependency             = 424
	TooEarly                     = 425
	UpgradeRequired              = 426
	PreconditionRequired         = 428
	TooManyRequests              = 429
	RequestHeaderFieldsTooLarge  = 431
	UnavailableForLegalReasons   = 451

	InternalServerError           = 500
	NotImplemented                = 501
	BadGateway                    = 502
	ServiceUnavailable            = 503
	GatewayTimeout                = 504
	HTTPVersionNotSupported       = 505
	VariantAlsoNegotiates         = 506
	InsufficientStorage           = 507
	LoopDetected                  = 508
	NotExtended                   = 510
	NetworkAuthenticationRequired = 511
)

Variables

View Source
var (
	Success             = NewError(http.StatusOK, CodeOk, "成功")
	ErrInvalidArgs      = NewError(BadRequest, CodeInvalidParam, "参数出错了(;一_一)")
	ErrArgs             = ErrInvalidArgs
	ErrNilArg           = ErrArgs.Msg("参数为空")
	ErrNotLogin         = NewError(Unauthorized, CodeUnauthorized, "请先登录一下吧~(≧▽≦)")
	ErrPermission       = NewError(Forbidden, CodeForbidden, "需要特殊通行证")
	ErrNotFound         = NewError(NotFound, CodeNotFound, "找不到你要的资源")
	ErrInternal         = NewError(InternalServerError, CodeInternal, "服务器被怪兽踢烂了(ノ`Д´)ノ")
	ErrDepNotReady      = NewError(InternalServerError, CodeInternal, "服务未就绪")
	ErrInternalPanic    = NewError(InternalServerError, CodeInternalPanic, "服务器炸掉了")
	ErrDuplicate        = NewError(InternalServerError, CodeDuplicate, "资源重复")
	ErrDataTooLong      = NewError(InternalServerError, CodeTooLong, "数据超长")
	ErrServerSigning    = NewError(InternalServerError, CodeServerSigningFail, "服务器签名失败")
	ErrResourceNotFound = NewError(NotFound, CodeResourceNotFound, "资源不存在")

	ErrCsrf               = NewError(Forbidden, CodeCsrfFailed, "CSRF校验失败")
	ErrServiceUnavailable = NewError(ServiceUnavailable, CodeServiceUnavailable, "服务暂不可用")
	ErrOther              = NewError(InternalServerError, CodeOther, "服务错误")
)

常用错误

View Source
var (
	ErrApiWentOffline = NewError(NotFound, CodeOther, "接口不存在(´・ω・`)")
	ErrPanic          = NewError(InternalServerError, -99999, "FATAL PANIC")
)

internal user

Functions

func Cause

func Cause(err error) error

func ErrorHandler

func ErrorHandler(err error) (int, any)

func ErrorHandlerCtx

func ErrorHandlerCtx(ctx context.Context, err error) (int, any)

func FramesWrapped

func FramesWrapped(err error) bool

func GrpcCodeFromHttpStatus

func GrpcCodeFromHttpStatus(status int) codes.Code

http-grpc状态码转换

func IsInternal

func IsInternal(err error) bool

func ResultHandler

func ResultHandler(ctx context.Context, data any) any

func ShouldLogError

func ShouldLogError(err error) bool

判断给定的err是否应该打印日志

func StripFrames

func StripFrames(err error) error

去除error中包含的堆栈信息

func UnwrapFrames

func UnwrapFrames(err error) stacktrace.Frames

从error中提取出堆栈信息

func UnwrapMsg

func UnwrapMsg(err error) (string, error)

从error中提取出msg信息

Types

type Causer

type Causer interface {
	Cause() error
}

type ErrProxy

type ErrProxy interface {
	error
	Stack() []*runtime.Frame
	Context() context.Context
	Fields() map[string]any
	Extra() map[string]any
	WithCtx(ctx context.Context) ErrProxy
	WithField(key string, val any) ErrProxy
	WithExtra(key string, val any) ErrProxy
	WithFields(kvs ...any) ErrProxy
	WithExtras(kvs ...any) ErrProxy
}

func Wrap

func Wrap(err error) ErrProxy

func Wrapf

func Wrapf(err error, format string, args ...any) ErrProxy

type Error

type Error struct {
	StatusCode int    `json:"stcode,omitempty"` // http响应状态码
	Code       int    `json:"code"`             // 业务响应码
	Message    string `json:"msg"`
}

func FromJson

func FromJson(c string) *Error

func NewError

func NewError(st, code int, msg string) *Error

func (*Error) AsResult

func (e *Error) AsResult() *Result

func (*Error) Equal

func (e *Error) Equal(other *Error) bool

func (*Error) ErrCode

func (e *Error) ErrCode(ecode int) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) ExtMsg

func (e *Error) ExtMsg(extmsg string) *Error

func (*Error) Is

func (e *Error) Is(err error) bool

func (*Error) Json

func (e *Error) Json() string

func (*Error) Msg

func (e *Error) Msg(msg string) *Error

func (*Error) Msgf

func (e *Error) Msgf(format string, args ...any) *Error

func (*Error) ShouldLogError

func (e *Error) ShouldLogError() bool

type Result

type Result struct {
	Code int    `json:"code"` // 业务响应状态码
	Msg  string `json:"msg"`
	Data any    `json:"data,omitempty"`
}

func NewResult

func NewResult(msg string, data any) *Result

type Unwrapper

type Unwrapper interface {
	Unwrap() error
}

Jump to

Keyboard shortcuts

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