Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is test #28

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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: 5 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.22
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.50.1
version: v1.59.0
args: --config .golangci.yaml
unit-test:
name: unit-test
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
- name: Set up Go 1.22
uses: actions/setup-go@v2
with:
go-version: 1.19
go-version: 1.22
- name: Check out code
uses: actions/checkout@v2
- name: unit-test
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ spellcheck:
echo "ERROR: cspell not found, install it manually! Link: https://cspell.org/docs/getting-started"; \
exit 1; \
fi

mod-tidy: ## run go mod tidy
go mod tidy
14 changes: 14 additions & 0 deletions eris.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package eris

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -95,6 +96,19 @@ func Errorf(format string, args ...any) statusError {
}
}

type joinError interface {
Unwrap() []error
}

// Join returns an error that wraps the given errors.
func Join(errs ...error) error {
internal := errors.Join(errs...)
if internal == nil {
return nil
}
return wrap(internal, "join error", DEFAULT_ERROR_CODE_NEW)
}

// Wrap adds additional context to all error types while maintaining the type of the original error. Adds a default error code 'internal'
//
// This method behaves differently for each error type. For root errors, the stack trace is reset to the current
Expand Down
30 changes: 30 additions & 0 deletions eris_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/risingwavelabs/eris"
"github.com/stretchr/testify/assert"
)

var (
Expand Down Expand Up @@ -938,3 +939,32 @@ func TestWrapType(t *testing.T) {
t.Errorf("expected nil error if wrap nil error, but error was %v", erisErr)
}
}

func TestJoinError(t *testing.T) {
err := eris.Join(nil, nil)
if err != nil {
t.Errorf("join nil should be nil")
}
err = eris.Join(nil, fmt.Errorf("external error"))
if err == nil {
t.Errorf("join error should be error")
}
err = eris.Join(fmt.Errorf("err1"), nil, fmt.Errorf("err2"))
if err == nil {
t.Errorf("join errors should be error")
}
type joinError interface {
Unwrap() []error
}
if joinErr, ok := eris.Unwrap(err).(joinError); !ok {
if len(joinErr.Unwrap()) != 2 {
t.Errorf("join 2 errors should be 2 errors")
}
}

err1 := errors.New("err1")
err2 := errors.New("err2")
err = eris.Join(err1, err2)
assert.True(t, errors.Is(err, err1))
assert.True(t, errors.Is(err, err2))
}
49 changes: 42 additions & 7 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eris

import (
"fmt"
"strings"
)

// FormatOptions defines output options like omitting stack traces and inverting the error or stack order.
Expand Down Expand Up @@ -103,8 +104,11 @@ func ToCustomString(err error, format StringFormat) string {
if format.Options.InvertOutput {
errSep := false
if format.Options.WithExternal && upErr.ErrExternal != nil {
str += formatExternalStr(upErr.ErrExternal, format.Options.WithTrace)
if (format.Options.WithTrace && len(upErr.ErrRoot.Stack) > 0) || upErr.ErrRoot.Msg != "" {
externalStr := formatExternalStr(upErr.ErrExternal, format.Options.WithTrace)
str += externalStr
if strings.Contains(externalStr, "\n") {
str += "\n"
} else if (format.Options.WithTrace && len(upErr.ErrRoot.Stack) > 0) || upErr.ErrRoot.Msg != "" {
errSep = true
str += format.ErrorSep
}
Expand All @@ -124,10 +128,13 @@ func ToCustomString(err error, format StringFormat) string {
}
str += upErr.ErrRoot.formatStr(format)
if format.Options.WithExternal && upErr.ErrExternal != nil {
if (format.Options.WithTrace && len(upErr.ErrRoot.Stack) > 0) || upErr.ErrRoot.Msg != "" {
externalStr := formatExternalStr(upErr.ErrExternal, format.Options.WithTrace)
if strings.Contains(externalStr, "\n") {
str += "\n"
} else if (format.Options.WithTrace && len(upErr.ErrRoot.Stack) > 0) || upErr.ErrRoot.Msg != "" {
str += format.ErrorSep
}
str += formatExternalStr(upErr.ErrExternal, format.Options.WithTrace)
str += externalStr
}
}

Expand Down Expand Up @@ -250,7 +257,17 @@ func ToCustomJSON(err error, format JSONFormat) map[string]any {

jsonMap := make(map[string]any)
if format.Options.WithExternal && upErr.ErrExternal != nil {
jsonMap["external"] = formatExternalStr(upErr.ErrExternal, format.Options.WithTrace)

join, ok := upErr.ErrExternal.(joinError)
if !ok {
jsonMap["external"] = formatExternalStr(upErr.ErrExternal, format.Options.WithTrace)
} else {
var externals []map[string]any
for _, e := range join.Unwrap() {
externals = append(externals, ToCustomJSON(e, format))
}
jsonMap["externals"] = externals
}
}

if upErr.ErrRoot.Msg != "" || len(upErr.ErrRoot.Stack) > 0 {
Expand Down Expand Up @@ -311,10 +328,28 @@ type UnpackedError struct {

// String formatter for external errors.
func formatExternalStr(err error, withTrace bool) string {
type joinError interface {
Unwrap() []error
}

format := "%v"
if withTrace {
return fmt.Sprintf("%+v", err)
format = "%+v"
}
join, ok := err.(joinError)
if !ok {
return fmt.Sprintf(format, err)
}

var strs []string
for i, e := range join.Unwrap() {
lines := strings.Split(fmt.Sprintf(format, e), "\n")
for no, line := range lines {
lines[no] = fmt.Sprintf("\t%s", line)
}
strs = append(strs, fmt.Sprintf("%d>", i)+strings.Join(lines, "\n"))
}
return fmt.Sprint(err)
return strings.Join(strs, "\n")
}

// ErrRoot represents an error stack and the accompanying message.
Expand Down
Loading