From 9e40b07fd7ba2355a72377aeed6819e480ab29d2 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Mon, 27 May 2019 13:10:14 +0200 Subject: [PATCH] backport parts of #17065 --- pkg/util/errutil/errors.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/util/errutil/errors.go b/pkg/util/errutil/errors.go index 37f26ff9564..612b68c40db 100644 --- a/pkg/util/errutil/errors.go +++ b/pkg/util/errutil/errors.go @@ -1,11 +1,28 @@ package errutil -import "golang.org/x/xerrors" +import ( + "fmt" + + "golang.org/x/xerrors" +) // Wrap is a simple wrapper around Errorf that is doing error wrapping. You can read how that works in // https://godoc.org/golang.org/x/xerrors#Errorf but its API is very implicit which is a reason for this wrapper. // There is also a discussion (https://github.com/golang/go/issues/29934) where many comments make arguments for such // wrapper so hopefully it will be added in the standard lib later. func Wrap(message string, err error) error { + if err == nil { + return nil + } return xerrors.Errorf("%v: %w", message, err) } + +// Wrapf is a simple wrapper around Errorf that is doing error wrapping +// Wrapf allows you to send a format and args instead of just a message. +func Wrapf(err error, message string, a ...interface{}) error { + if err == nil { + return nil + } + + return Wrap(fmt.Sprintf(message, a...), err) +}