Skip to content

Commit

Permalink
Merge pull request #34 from gemcook/feature/add-readme
Browse files Browse the repository at this point in the history
feat: add readme
  • Loading branch information
ss49919201 committed Nov 8, 2021
2 parents 55913b8 + 0182fb9 commit b3c70c9
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 13 deletions.
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# merr

[![Analysis](https://github.com/gemcook/merr/actions/workflows/analysis.yml/badge.svg)](https://github.com/gemcook/merr/actions/workflows/analysis.yml) ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/gemcook/merr)

`merr` is a package that binds multiple errors.

## Features

`merr` has pretty print, which outputs the bound errors in a well-formatted and easy-to-understand format.
It is also possible to change the output destination if necessary, which is very useful for debugging.

## Installation

```sh
go get -u github.com/gemcook/merr
```

## Usage

```go
import "github.com/gemcook/merr"
```

### Appends error

Appends error to list of errors.
If there is no error list, it creates a new one.

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

for i := 0; i < 10; i++ {
if err := something(); err != nil {
multiError.Append(err)
}
}
```

### Print list of error

Prints the object of the list of errors, separated by `,\n`.

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

for i := 0; i < 10; i++ {
// something() returns &errors.errorString{s: "something error"}
if err := something(); err != nil {
multiError.Append(err)
}
}

fmt.Println(multiError.Error())
```

```
"something error",
"something error",
.
.
.
"something error"
```

### Pretty print

Prints a list of errors in a well-formatted, easy-to-understand format.

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

for i := 0; i < 10; i++ {
// something() returns &errors.errorString{s: "something error"}
if err := something(); err != nil {
multiError.Append(err)
}
}

multiError.PrettyPrint()
```

```
Errors[
&errors.errorString{
s: "something error",
},
&errors.errorString{
s: "something error",
},
&errors.errorString{
s: "something error",
},
.
.
.
&errors.errorString{
s: "something error",
},
]
```

The default output destination is the **standard output**.
You can also change the output destination.

```go
buf := bytes.NewBuffer(nil)
merr.SetOutput(buf)
```

## License

MIT License
4 changes: 2 additions & 2 deletions errs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errs
package merr

import (
"bytes"
Expand Down Expand Up @@ -27,7 +27,7 @@ var (

var formatter = func(e *errs) string {
var result string
suffix := ",\n"
suffix := "\n"
for i, err := range e.Errors {
if len(e.Errors)-1 == i {
suffix = ""
Expand Down
8 changes: 4 additions & 4 deletions errs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errs
package merr

import (
"bytes"
Expand Down Expand Up @@ -35,7 +35,7 @@ func Test_errs_Error(t *testing.T) {
fmt.Errorf("%s", "error2"),
},
},
"error1,\nerror2",
"error1\nerror2",
},
{
"check/error/wrapped",
Expand All @@ -46,7 +46,7 @@ func Test_errs_Error(t *testing.T) {
fmt.Errorf("%w", &somethingError{}),
},
},
"something error,\nsomething error",
"something error\nsomething error",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -191,7 +191,7 @@ func Test_errs_PrettyPrint(t *testing.T) {
&somethingError{},
},
},
"Errors[\n &errs.somethingError{},\n]",
"Errors[\n &merr.somethingError{},\n]",
},
{
"print nil",
Expand Down
12 changes: 6 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package errs_test
package merr_test

import (
"fmt"
"os"

"github.com/gemcook/errs"
"github.com/gemcook/merr"
)

type structError struct {
Expand All @@ -28,7 +28,7 @@ func (*ptrError) Error() string {
}

func Example() {
err := errs.New()
err := merr.New()

// error interface
err.Append(fmt.Errorf("%w", fmt.Errorf("wrap error")))
Expand All @@ -49,7 +49,7 @@ func Example() {
}
err.Append(ptrErr)

errs.SetOutput(os.Stdout)
merr.SetOutput(os.Stdout)
err.PrettyPrint()

// output:
Expand All @@ -60,12 +60,12 @@ func Example() {
// s: "wrap error",
// },
// },
// errs_test.structError{
// merr_test.structError{
// i: 1,
// str: "error",
// b: true,
// },
// &errs_test.ptrError{
// &merr_test.ptrError{
// i: 1,
// str: "error",
// b: true,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gemcook/errs
module github.com/gemcook/merr

go 1.17

Expand Down

0 comments on commit b3c70c9

Please sign in to comment.