status

package
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

Status is used to unified errors between services, no matter is http/rpc.

The code is 60% copied from the official gRPC status package.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// predefined global status, it can be override.
	OK                 = add(0, "ok")
	RequestErr         = add(400, "错误请求")
	Unauthorized       = add(401, "未认证,请先登录")
	AccessDenied       = add(403, "未授权访问")
	NothingFound       = add(404, "页面不存在")
	MethodNotAllowed   = add(405, "不支持该方法")
	LimitExceed        = add(429, "请勿频繁请求")
	Canceled           = add(498, "客户端取消请求")
	ServerErr          = add(500, "网络错误,请稍后重试")
	ServiceUnavailable = add(503, "过载保护,服务暂不可用")
	Deadline           = add(504, "服务调用超时")
	AppKeyInvalid      = add(600, "应用程序不存在或已被封禁")
	SignCheckErr       = add(601, "签名校验失败")
	RepeatedRequest    = add(602, "重复请求")
	CaptchaErr         = add(603, "验证码错误")
	TargetBlocked      = add(604, "资源锁定中,请稍后重试")
	PayloadTooLarge    = add(605, "请求体大小超出限制")
	ServiceUpdate      = add(606, "系统升级中")
	UndefinedErr       = add(1000, "未知错误")
)

Functions

func EqualError added in v1.0.0

func EqualError(s *Status, err error) bool

EqualError check an err is a status error

Types

type Status

type Status struct {
	// contains filtered or unexported fields
}

Status represents an RPC status code, message, and details. It is immutable and should be created with New, Newf, or FromProto.

func Ephemeral added in v1.3.7

func Ephemeral(code int, msg string) *Status

Ephemeral returns a ephemeral Status instance for once

func ExtractContextStatus

func ExtractContextStatus(err error) *Status

ExtractContextStatus converts a context error into a Status. It returns a Status with status.OK if err is nil, or a Status from errToStatus if err is non-nil and not a context error.

func ExtractStatus

func ExtractStatus(err error) *Status

ExtractStatus extract status from grpc call reply err

func FromProto

func FromProto(s *spb.Status) *Status

FromProto returns a Status representing s.

func New

func New(code int, msg string) *Status

New returns a Status instance

func (*Status) Code

func (s *Status) Code() int

Code returns the status code contained in s.

func (*Status) Details

func (s *Status) Details() []interface{}

Details returns a slice of details messages attached to the status. If a detail cannot be decoded, the error is returned in place of the detail.

func (*Status) Error

func (s *Status) Error() string

Error implement error interface, return err code

func (*Status) Message

func (s *Status) Message() string

Message returns the message contained in s.

func (*Status) Proto

func (s *Status) Proto() *spb.Status

Proto returns s's status as an spb.Status proto message.

func (*Status) WithDetails

func (s *Status) WithDetails(details ...proto.Message) (*Status, error)

WithDetails returns a new status with the provided details messages appended to the status. If any errors are encountered, it returns nil and the first error encountered.

Jump to

Keyboard shortcuts

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