errors

一个Go语言的错误处理工具库,支持错误堆栈、错误码和错误链功能,简化错误处理流程并提升调试效率。

Check out errors on CurateClick

• Copy the embed code to showcase this product on your website

• Share on X to spread the word about this amazing tool

https://github.com/morrisxyang/errorsREADME# errorsGo ReferenceStatic BadgeCoverage StatusGo Report CardStatic Badge简单的支持错误堆栈, 错误码, 错误链的工具库:- 支持携带堆栈, 嵌套构造错误链- 支持携带错误码, 方便接口返回- 支持自定义堆栈打印深度和错误链打印格式- 使用 CallersFrames 替代 FuncForPC 生成堆栈, 避免特殊情况line number错误等问题, 详见runtime: strongly encourage using CallersFrames over FuncForPC with Callers result- 简化堆栈信息, 一条链路多次Wrap操作只保留最深层堆栈, 只打印一次## 安装和文档安装使用 go get github.com/morrisxyang/errors文档地址是 https://pkg.go.dev/github.com/morrisxyang/errors## 快速开始构造错误链gofunc a() error {err := b()err = Wrap(err, "a failed reason")return err}func b() error {err := c()err = Wrap(err, "b failed reason")return err}func c() error {_, err := os.Open("test")if err != nil {return WrapWithCode(err, 123, "c failed reason")}return nil}打印错误信息, %+v会打印堆栈, %v只打印错误信息goa failed reasonCaused by: b failed reasonCaused by: 123, c failed reasonCaused by: open test: no such file or directorygithub.com/morrisxyang/errors.c/Users/morrisyang/Nutstore Files/go-proj/githuberrors/errors_test.go:94github.com/morrisxyang/errors.b/Users/morrisyang/Nutstore Files/go-proj/githuberrors/errors_test.go:86github.com/morrisxyang/errors.a/Users/morrisyang/Nutstore Files/go-proj/githuberrors/errors_test.go:80....堆栈信息省略## 核心方法### 错误封装- func New(msg string) error- func Newf(format string, args ...interface{}) error- func NewWithCode(code int, msg string) error- func NewWithCodef(code int, format string, args ...interface{}) error- func Wrap(e error, msg string) error- func Wrapf(e error, format string, args ...interface{}) error- func WrapWithCode(e error, code int, msg string) error- func WrapWithCodef(e error, code int, format string, args ...interface{}) error### 错误解析- func Code(e error) int- func EffectiveCode(e error) int- func Msg(e error) string- func As(err error, target interface{}) bool- func Is(err, target error) bool- func Cause(e error) error- func Unwrap(err error) error### 配置- type Config- - func GetCfg() *Config- func ResetCfg()- func SetCfg(c *Config)## FAQ1. 多次 Wrap 错误会携带多次堆栈吗? 可在调用链路上多次Wrap, 添加说明信息, 但只有最深层的Wrap操作会设置堆栈, 继续 Wrap, return err 等操作不会影响堆栈信息2. 在链路中某个错误设置了合适的错误码, 然后继续Wrap时没有设置, 如何获取? 建议在合适的清晰的时机设置有效的错误码, 可以使用EffectiveCode获取链路中外层第一个有效的非0错误码, 由于系统调用等情况, 同一链路中可能有多个错误携带错误码, 此时默认外层的错误码应该对外暴露, 屏蔽了内层的详细信息.

Latest Weekly Picks

make.ink favicon
Weekly Pick

Your AI tattoo generator for pro-grade concepts

Leo Wade
sellfy.com favicon
Weekly Pick

A code-free online store builder to turn views into revenue. Sell digital products, subscriptions, and merch, without fees or hassle.

Maris
www.videotoblog.ai favicon
Weekly Pick

Convert videos into awesome blog posts.

Video To Blog
www.sellerpic.ai favicon
Weekly Pick

SellerPic is the all-in-one AI design platform for e-commerce

SellerPic
fastimage.ai favicon
Weekly Pick

Fast Image AI instantly transforms your photos into stunning styles like Ghibli, Sketch, and Pixar. Effortlessly control image elements and create amazing effects with just one click.

Fast AI Team
linkedinpro.daisy.so favicon
Weekly Pick

AI-powered tool that transforms casual photos into professional LinkedIn headshots instantly. No photographer needed—just upload and download.

Gabriel
crevas.ai favicon
Weekly Pick

Crevas unifies Veo 3, Sora 2, Nano Banana and more into one intuitive canvas — so filmmakers can script, prompt, and generate cinematic stories without switching tools or losing consistency.

Spark Alpha
fotominiatur.com favicon
Weekly Pick

AI Foto Edit - Text to Image & Image Edit

foto miniatur