xerrors

package
v0.2.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: MIT Imports: 5 Imported by: 0

README

This package provides a convenient way to map custom errors to both HTTP and gRPC status codes. This helps in ensuring consistency in error handling across different service architectures.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

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

Code is the type that represents an error code. It can map to HTTP and gRPC codes. In order to properly work with custom codes or code overrides: use the RegisterCode function after creating your new Code instance.

var (
	OK Code = Code{codes.OK, http.StatusOK, ""}
	// Unknown is the default error code.
	Unknown Code = Code{codes.Unknown, http.StatusInternalServerError, "Unknown"}
	// Internal is used when an internal error occurs.
	Internal Code = Code{codes.Internal, http.StatusInternalServerError, "InternalError"}
	// InvalidArgument is used when the client sends invalid arguments.
	InvalidArgument Code = Code{codes.InvalidArgument, http.StatusBadRequest, "InvalidArgument"}
	// NotFound is used when the requested resource is not found.
	NotFound Code = Code{codes.NotFound, http.StatusNotFound, "NotFound"}
	// AlreadyExists is used when the resource already exists.
	AlreadyExists Code = Code{codes.AlreadyExists, http.StatusConflict, "AlreadyExist"}
	// Unauthorized is used when the client is not authenticated.
	Unauthenticated Code = Code{codes.Unauthenticated, http.StatusUnauthorized, "Unauthenticated"}
	// PermissionDenied is used when the client is not authorized to perform the requested operation.
	PermissionDenied Code = Code{codes.PermissionDenied, http.StatusForbidden, "PermissionDenied"}
)

func NewCode

func NewCode(grpcCode codes.Code, httpCode int, reason string) Code

func (Code) GRPC

func (c Code) GRPC() codes.Code

GRPC returns the gPRC code that is mapped to the code.

func (Code) HTTP

func (c Code) HTTP() int

HTTP returns the HTTP code that is mapped to the code.

func (Code) MarshalJSON

func (c Code) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface and defines how a Code should be marshaled to JSON. By default, it marshals to a string representation defined by String function.

func (Code) R

func (c Code) R(reason string) Code

func (Code) String

func (c Code) String() string

String returns the string representation of the code.

type Errno

type Errno struct {
	Code      Code
	Message   string
	RequestID string
}

Errno 定义了 miniblog 使用的错误类型.

func New

func New(code Code, format string, args ...any) *Errno

func Parse

func Parse(err error) *Errno

func (*Errno) Error

func (err *Errno) Error() string

Error 实现 error 接口中的 `Error` 方法.

func (Errno) GRPCStatus

func (err Errno) GRPCStatus() *status.Status

This member function is used by grpc when converting an error into a status

func (Errno) HTTPStatus

func (err Errno) HTTPStatus() int

func (*Errno) Is

func (err *Errno) Is(target error) bool

func (*Errno) M

func (err *Errno) M(format string, args ...any) *Errno

M 设置 Errno 类型错误中的 Message 字段. 注意这里是直接替换 err.Message 的内容.

func (*Errno) SetRequestID

func (err *Errno) SetRequestID(requestID string) *Errno

Jump to

Keyboard shortcuts

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