From 95d8c65576aa10edeb63edce41eda5713b3af9e9 Mon Sep 17 00:00:00 2001 From: Stepan Pyzhov Date: Tue, 25 Feb 2020 17:05:46 +0100 Subject: [PATCH] add Wrap function --- .travis.yml | 2 -- README.md | 14 +++++++++++++- wrap.go | 11 +++++++++++ wrap_example_test.go | 14 ++++++++++++++ wrap_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 wrap.go create mode 100644 wrap_example_test.go create mode 100644 wrap_test.go diff --git a/.travis.yml b/.travis.yml index f008a0b..fed01f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/README.md b/README.md index ebf2df6..2adc907 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/wrap.go b/wrap.go new file mode 100644 index 0000000..953d3cb --- /dev/null +++ b/wrap.go @@ -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 +} diff --git a/wrap_example_test.go b/wrap_example_test.go new file mode 100644 index 0000000..e0b54d4 --- /dev/null +++ b/wrap_example_test.go @@ -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 +} diff --git a/wrap_test.go b/wrap_test.go new file mode 100644 index 0000000..23287dc --- /dev/null +++ b/wrap_test.go @@ -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) + } + }) + } +}