-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runutil: Add CloseWithLogOnErr function
Signed-off-by: Arve Knudsen <[email protected]>
- Loading branch information
Showing
5 changed files
with
96 additions
and
16 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 was deleted.
Oops, something went wrong.
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,31 @@ | ||
package runutil | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/grafana/dskit/multierror" | ||
) | ||
|
||
// CloseWithErrCapture closes closer and wraps any error with the provided message and assigns it to err. | ||
func CloseWithErrCapture(err *error, closer io.Closer, msg string) { | ||
merr := multierror.New(*err, errors.Wrap(closer.Close(), msg)) | ||
*err = merr.Err() | ||
} | ||
|
||
// CloseWithLogOnErr closes an io.Closer and logs any relevant error from it wrapped with the provided format string and | ||
// args. | ||
func CloseWithLogOnErr(logger log.Logger, closer io.Closer, format string, args ...interface{}) { | ||
err := closer.Close() | ||
if err == nil || errors.Is(err, os.ErrClosed) { | ||
return | ||
} | ||
|
||
msg := fmt.Sprintf(format, args...) | ||
level.Warn(logger).Log("msg", "detected close error", "err", fmt.Sprintf("%s: %s", msg, err.Error())) | ||
} |
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,62 @@ | ||
package runutil | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-kit/log/level" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCloseWithLogOnErr(t *testing.T) { | ||
t.Run("With non-close error", func(t *testing.T) { | ||
closer := fakeCloser{err: fmt.Errorf("an error")} | ||
logger := fakeLogger{} | ||
|
||
CloseWithLogOnErr(&logger, closer, "closing failed") | ||
|
||
assert.Empty(t, cmp.Diff([]interface{}{ | ||
"level", level.WarnValue(), "msg", "detected close error", "err", "closing failed: an error", | ||
}, logger.keyvals, cmp.Comparer(func(a, b fmt.Stringer) bool { | ||
t.Logf("Comparing %q and %q", a.String(), b.String()) | ||
return a.String() == b.String() | ||
}))) | ||
}) | ||
|
||
t.Run("With no error", func(t *testing.T) { | ||
closer := fakeCloser{} | ||
logger := fakeLogger{} | ||
|
||
CloseWithLogOnErr(&logger, closer, "closing failed") | ||
|
||
assert.Empty(t, logger.keyvals) | ||
}) | ||
|
||
t.Run("With closed error", func(t *testing.T) { | ||
closer := fakeCloser{err: os.ErrClosed} | ||
logger := fakeLogger{} | ||
|
||
CloseWithLogOnErr(&logger, closer, "closing failed") | ||
|
||
assert.Empty(t, logger.keyvals) | ||
}) | ||
} | ||
|
||
type fakeCloser struct { | ||
err error | ||
} | ||
|
||
func (c fakeCloser) Close() error { | ||
return c.err | ||
} | ||
|
||
type fakeLogger struct { | ||
keyvals []interface{} | ||
} | ||
|
||
func (l *fakeLogger) Log(keyvals ...interface{}) error { | ||
l.keyvals = keyvals | ||
return nil | ||
} |