Skip to content

Commit

Permalink
Merge pull request #2 from Cleverse/feature/package-errors
Browse files Browse the repository at this point in the history
Package Errors
  • Loading branch information
Planxnx authored Oct 19, 2023
2 parents 7d4a2be + af29a16 commit 0402d90
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions errors/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![GoDoc](https://godoc.org/github.com/Cleverse/go-utilities/errors?status.svg)](http://godoc.org/github.com/Cleverse/go-utilities/errors)
[![Report card](https://goreportcard.com/badge/github.com/Cleverse/go-utilities/errors)](https://goreportcard.com/report/github.com/Cleverse/go-utilities/errors)

# errors

Package errors adds stacktrace support to errors in go.
Expand Down
7 changes: 3 additions & 4 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package errors

import (
"errors"
stderrors "errors"
"fmt"
"io"
Expand All @@ -20,7 +19,7 @@ func TestNew(t *testing.T) {
want error
}{
{"new error", New("new error")},
{"with format: %v", errors.New("with format: %v")},
{"with format: %v", stderrors.New("with format: %v")},
}

for _, testcase := range testcases {
Expand Down Expand Up @@ -48,7 +47,7 @@ func TestErrorf(t *testing.T) {
}, {
"with format: %v",
[]interface{}{"value"},
errors.New("with format: value"),
stderrors.New("with format: value"),
}}

for _, testcase := range testcases {
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestFormatNew(t *testing.T) {
[]string{
"error\n",
"github.com/Cleverse/go-utilities/errors.TestFormatNew\n",
"errors/errors_test.go:81",
"errors/errors_test.go:80",
},
}, {
New("error"),
Expand Down
5 changes: 5 additions & 0 deletions errors/join.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build go1.20
// +build go1.20

package errors

import (
Expand All @@ -17,3 +20,5 @@ import (
func Join(errs ...error) error {
return errors.WithStack(stderrors.Join(errs...), 1)
}

// TODO: implement joinError.Format method for stack traces printing
61 changes: 61 additions & 0 deletions errors/join_backward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//go:build !go1.20
// +build !go1.20

package errors

import errors "github.com/Cleverse/go-utilities/errors/errconstructor"

// Disclaimer: functions Join is copied from the stdlib errors
// package v1.21.3

// Join returns an error that wraps the given errors with a stack trace at the point WithStack was called.
// Any nil error values are discarded.
// Join returns nil if every value in errs is nil.
// The error formats as the concatenation of the strings obtained
// by calling the Error method of each element of errs, with a newline
// between each string.
//
// A non-nil error returned by Join implements the Unwrap() []error method.
func Join(errs ...error) error {
n := 0
for _, err := range errs {
if err != nil {
n++
}
}
if n == 0 {
return nil
}
e := &joinError{
// stack: errors.Callers(0),
errs: make([]error, 0, n),
}
for _, err := range errs {
if err != nil {
e.errs = append(e.errs, err)
}
}
return errors.WithStack(e, 1)
}

type joinError struct {
// stack *errors.Stacks
errs []error
}

func (e *joinError) Error() string {
var b []byte
for i, err := range e.errs {
if i > 0 {
b = append(b, '\n')
}
b = append(b, err.Error()...)
}
return string(b)
}

func (e *joinError) Unwrap() []error {
return e.errs
}

// TODO: implement joinError.Format method for stack traces printing
3 changes: 3 additions & 0 deletions errors/wrap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build go1.13
// +build go1.13

package errors

import (
Expand Down

0 comments on commit 0402d90

Please sign in to comment.