Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# errors
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpub-go%2Ferrors.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fpub-go%2Ferrors?ref=badge_shield)


errors with stack, supports multi-cause error format as tree,
inspired by [cockroachdb/errors](github.com/cockroachdb/errors)

> 博客:[造一个 Go 语言错误库的轮子](https://youthlin.com/?p=1868)

[![sync-to-gitee](https://github.com/pub-go/errors/actions/workflows/gitee.yaml/badge.svg)](https://github.com/pub-go/errors/actions/workflows/gitee.yaml)
[![test](https://github.com/pub-go/errors/actions/workflows/test.yaml/badge.svg)](https://github.com/pub-go/errors/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/pub-go/errors/graph/badge.svg?token=OfZF3kQlBS)](https://codecov.io/gh/pub-go/errors)
[![Go Report Card](https://goreportcard.com/badge/code.gopub.tech/errors)](https://goreportcard.com/report/code.gopub.tech/errors)
[![Go Reference](https://pkg.go.dev/badge/code.gopub.tech/errors.svg)](https://pkg.go.dev/code.gopub.tech/errors)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpub-go%2Ferrors.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fpub-go%2Ferrors?ref=badge_shield)

Example
```go
package main
Expand Down
7 changes: 6 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ func WithStack(err error) error {
}
}

// Join 聚合多个错误
func Join(errs ...error) error {
err := join(errs...)
if err == nil {
return nil
}
return &withStack{
error: join(errs...),
error: err,
stack: callers(),
}
}
Expand Down
6 changes: 6 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func TestWithStack(t *testing.T) {
}

func TestJoin(t *testing.T) {
if err := errors.Join(); err != nil {
t.Errorf("Join() want nil, got: %+v", err)
}
if err := errors.Join(nil); err != nil {
t.Errorf("Join(nil) want nil, got: %+v", err)
}
print(t, errors.Join(errFmt, errLeafNew))
}

Expand Down
5 changes: 5 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func Formattable(err error) FormattableError {
return &errorFormatter{err}
}

// Detail 先将 err 包装为 Formattable 再使用 %+v 格式化为字符串
func Detail(err error) string {
return fmt.Sprintf("%+v", Formattable(err))
}

// FormatError 能够识别格式化动词智能打印。
// 当一个错误类型在实现 fmt.Formatter 接口时,可以直接转发给本函数。
//
Expand Down
20 changes: 20 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors_test

import (
"fmt"
"testing"

"code.gopub.tech/errors"
Expand All @@ -16,3 +17,22 @@ func TestFormat(t *testing.T) {
err1234 := errors.Join(err12, err34)
t.Logf("%+v", err1234)
}

func TestDetail(t *testing.T) {
if s := errors.Detail(nil); s != "<nil>" {
t.Errorf("Detail(nil) got %s", s)
}
t.Logf("%s", errors.Detail(errors.New("error")))
e0 := fmt.Errorf("fmtErr")
err := errors.F(e0)
if e, ok := err.(interface{ Cause() error }); ok {
if e.Cause() != e0 {
t.Errorf("Cause() failed")
}
}
if e, ok := err.(interface{ Unwrap() error }); ok {
if e.Unwrap() != e0 {
t.Errorf("Unwrap() failed")
}
}
}