-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |