errs

package module
v0.0.0-...-0025b7e Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 12 Imported by: 7

Documentation

Overview

Package errs 实现基于 tRPC errs 的错误功能封装

Index

Constants

View Source
const (
	// Filter 名称
	ErrToCodeFilterName = "err_to_code"
	CodeToFilterName    = "code_to_err"
	// Plugin 配置类型, 用于指定 json tag
	ErrToCodePluginType = "filter"
	// Plugin 配置名
	ErrToCodePluginName = "err_to_code"
)

Variables

View Source
var (
	// UndefinedError 表示未定义错误
	UndefinedError = errs.New(999, "undefined error").(*errs.Error)
)

Functions

func ExtractCodeMessage

func ExtractCodeMessage[T constraints.Integer](err error) (code T, msg string)

ExtractCodeMessage 提取错误中的 code 和描述, 信息不摘要, 只保留可显示的 msg

func ExtractCodeMessageDigest

func ExtractCodeMessageDigest[T constraints.Integer](err error) (code T, msg string)

ExtractCodeMessageDigest 提取错误中的 code 和描述, 如果是对 trpc errs 的 wrapping, 则在 message 之后对完整信息进行摘要。

func New

func New[T errs.ErrCode](code T, msg string) error

New 是对 tRPC errs New 的简单封装, 避免引入两个同名 package

func RegisterErrToCodeFilter

func RegisterErrToCodeFilter()

RegisterErrToCodeFilter 注册 err_to_code filter, 该 filter 可以将 tRPC 函数实现中 返回的 err 类型转换为 response 中的 code / message 字段, 也可以作为 client filter, 将 code / message 数据提取出来, 放在 err 字段中, 让 RPC 的调用方和实现方对 RPC 可以有 如函数调用般的体验, 可以只判断 error 类型字段而无需再判断 rsp 中的 code 字段。

必须在 trpc.NewServer 之前调用

func SetDigestDescription

func SetDigestDescription(desc string)

SetDigestDescription 当不暴露具体错误, 但给出摘要值时, 摘要值的描述名是什么 (默认是 "digest code")

func SetDigestFunction

func SetDigestFunction(f func(error) string)

SetDigestFunction 设置错误描述的摘要函数。默认值为 github.com/Andrew-M-C/go.util/errors.ErrorToCode

func SetServerErrorCodeMsgSetter

func SetServerErrorCodeMsgSetter(fu ErrorCodeMsgSetterFunc)

SetServerErrorCodeMsgSetter 设置在 err_to_code filter 中, 当提取出错误时, 的回调函数。 业务方可以使用这个回调进行 set 操作。如果没有这个 setter 的话, filter 会自行使用反射进行设置。

返回 true 表示已经设置 OK, 否则 filter 会继续往下走继续使用反射逻辑进行设置

Types

type ErrorCodeMsgSetterFunc

type ErrorCodeMsgSetterFunc func(ctx context.Context, rsp any, code int, msg string) bool

ErrorCodeMsgSetterFunc 表示当提取出错误时, 可以回调给业务方进行 set 操作。如果没有这个 setter 的话, filter 会自行使用反射进行设置。

返回 true 表示已经设置 OK, 否则 filter 会继续往下走继续使用反射逻辑进行设置

Jump to

Keyboard shortcuts

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