Skip to content

Commit

Permalink
add Wrap function (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov authored Feb 25, 2020
1 parent 23b8a83 commit a1a99b7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ language: go
# You don't need to test on very old version of the Go compiler. It's the user's
# responsibility to keep their compilers up to date.
go:
- 1.11.x
- 1.12.x
- 1.13.x

env:
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![GoDoc](https://godoc.org/github.com/spyzhov/safe?status.svg)](https://godoc.org/github.com/spyzhov/safe)
[![Coverage Status](https://coveralls.io/repos/github/spyzhov/safe/badge.svg?branch=master)](https://coveralls.io/github/spyzhov/safe?branch=master)

Golang Minimal Version: **1.13**

Package safe is a minimal project with safe methods to write clean code.

## safe.Close
Expand Down Expand Up @@ -36,12 +38,22 @@ Full description of the problem in article ["Go-tcha: When nil != nil"](https://

```go
func Foo(str fmt.Stringer) {
if !IsNil(str) {
if !safe.IsNil(str) {
// ...
}
}
```

## safe.Wrap

`Wrap` will wrap current error with the scope value, or return `nil` if error wasn't set.

```go
func Unmarshal(data []byte) (value interface{}, err error) {
return value, safe.Wrap(json.Unmarshal(data, &value), "broken json")
}
```

# License

Licensed under the MIT license, see [LICENSE](LICENSE) for details.
11 changes: 11 additions & 0 deletions wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package safe

import "fmt"

// Wrap will wrap current error with the scope value, or return nil if error wasn't set.
func Wrap(err error, scope string) error {
if !IsNil(err) {
return fmt.Errorf("%s: %w", scope, err)
}
return nil
}
14 changes: 14 additions & 0 deletions wrap_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package safe

import (
"encoding/json"
"fmt"
)

func ExampleWrap() {
var value interface{}
err := Wrap(json.Unmarshal([]byte(`[broken`), &value), "broken json")
fmt.Println(err)
// Output:
// broken json: invalid character 'b' looking for beginning of value
}
43 changes: 43 additions & 0 deletions wrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package safe

import (
"errors"
"fmt"
"testing"
)

func TestWrap(t *testing.T) {
simple := fmt.Errorf("error")
type args struct {
err error
scope string
}
tests := []struct {
name string
args args
result error
}{
{
name: "nil",
args: args{nil, ""},
result: nil,
},
{
name: "nil/scope",
args: args{nil, "scope"},
result: nil,
},
{
name: "error/scope",
args: args{simple, "scope"},
result: simple,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Wrap(tt.args.err, tt.args.scope); err != tt.result && !errors.Is(err, tt.result) {
t.Errorf("Wrap() error = %v, wanted %v", err, tt.result)
}
})
}
}

0 comments on commit a1a99b7

Please sign in to comment.