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

feat: add ErrorOrNil #41

Merged
merged 2 commits into from
Dec 14, 2021
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ go get -u github.com/gemcook/merr
import "github.com/gemcook/merr"
```

### Determine if it's an error

```go
multiError := merr.New()

// do something...

// error handling example
if multiError.ErrorOrNil() != nil {
return multiError
}
return nil
```

### Appends error

Appends error to list of errors.
Expand Down Expand Up @@ -53,7 +67,7 @@ for i := 0; i < 10; i++ {
fmt.Println(multiError.Error())
```

```
```text
"something error",
"something error",
.
Expand All @@ -79,7 +93,7 @@ for i := 0; i < 10; i++ {
multiError.PrettyPrint()
```

```
```text
Errors[
&errors.errorString{
s: "something error",
Expand Down
36 changes: 36 additions & 0 deletions errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func SetNewLine(n string) {
type Errs interface {
Append(err error)
Error() string
ErrorOrNil() error
Is(target error) bool
As(target interface{}) bool
PrettyPrint()
Expand All @@ -90,7 +91,30 @@ func (e *errs) Error() string {
return formatter(e)
}

func (e *errs) ErrorOrNil() error {
if e == nil {
return nil
}

if len(e.Errors) == 0 {
return nil
}

// If only nil is contained in the array,
// it is not considered an error.
for _, err := range e.Errors {
if err != nil {
return e
}
}

return nil
}

func (e *errs) Append(err error) {
if e == nil {
return
}
e.mx.Lock()
defer e.mx.Unlock()
if e.Errors == nil {
Expand All @@ -100,6 +124,9 @@ func (e *errs) Append(err error) {
}

func (e *errs) Is(target error) bool {
if e == nil {
return false
}
for _, err := range e.Errors {
if errors.Is(err, target) {
return true
Expand All @@ -109,6 +136,9 @@ func (e *errs) Is(target error) bool {
}

func (e *errs) As(target interface{}) bool {
if e == nil {
return false
}
for _, err := range e.Errors {
if errors.As(err, target) {
return true
Expand All @@ -118,6 +148,12 @@ func (e *errs) As(target interface{}) bool {
}

func (e *errs) PrettyPrint() {
if e == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

badge
This logic is not work. ( It's not a bug, but a fundamental problem that needs to be solved. )

e = &errs{
mx: sync.Mutex{},
Errors: nil,
}
}
fmt.Fprint(output, e.prettyFormat())
}

Expand Down
48 changes: 44 additions & 4 deletions errs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/stretchr/testify/assert"
)

type somethingError struct{}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just curious about the order.


func (s *somethingError) Error() string { return "something error" }

func Test_errs_Error(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -173,10 +177,6 @@ func Test_errs_As(t *testing.T) {
}
}

type somethingError struct{}

func (s *somethingError) Error() string { return "something error" }

func Test_errs_PrettyPrint(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -266,3 +266,43 @@ func TestSetNewLine(t *testing.T) {
})
}
}

func Test_errs_ErrorOrNil(t *testing.T) {
tests := []struct {
name string
errs []error
wantErr bool
}{
{
"an error",
[]error{
&somethingError{},
},
true,
},
{
"no error",
[]error{},
false,
},
{
"nil only",
[]error{nil},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := New()
for _, e := range tt.errs {
err.Append(e)
}

if tt.wantErr {
assert.Error(t, err.ErrorOrNil())
} else {
assert.NoError(t, err.ErrorOrNil())
}
})
}
}