Documentation ¶
Overview ¶
Initially copied from Thanos
Copyright (c) The Thanos Authors. Licensed under the Apache License 2.0.
Package errcapture is useful when you want to close a `Closer` interface. As we all know, we should close all implements of `Closer`, such as *os.File. Commonly we will use:
defer closer.Close()
The problem is that Close() usually can return important error e.g for os.File the actual file flush might happen (and fail) on `Close` method. It's important to *always* check error. Thanos provides utility functions to log every error like those, allowing to put them in convenient `defer`:
defer errcapture.CloseWithLog(logger, closer, "log format message")
For capturing error, use Close:
var err error defer errcapture.Close(&err, closer, "log format message") // ...
If Close() returns error, err will capture it and return by argument.
The errcapture.Exhaust* family of functions provide the same functionality but they take an io.ReadCloser and they exhaust the whole reader before closing them. They are useful when trying to use http keep-alive connections because for the same connection to be re-used the whole response body needs to be exhausted.
Index ¶
- func Close(err *error, closer closerFunc, format string, a ...interface{})
- func CloseWithLog(logger Logger, closer closerFunc, format string, a ...interface{})
- func ExhaustClose(err *error, r io.ReadCloser, format string, a ...interface{})
- func ExhaustCloseWithLog(logger Logger, r io.ReadCloser, format string, a ...interface{})
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶
Close runs function and on error return error by argument including the given error (usually from caller function).
func CloseWithLog ¶
CloseWithLog is making sure we log every error, even those from best effort tiny closers.
func ExhaustClose ¶
func ExhaustClose(err *error, r io.ReadCloser, format string, a ...interface{})
ExhaustClose closes the io.ReadCloser with error capture but exhausts the reader before.
func ExhaustCloseWithLog ¶
func ExhaustCloseWithLog(logger Logger, r io.ReadCloser, format string, a ...interface{})
ExhaustCloseWithLog closes the io.ReadCloser with a log message on error but exhausts the reader before.