errcapture

package
v0.0.0-...-e2b8481 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2020 License: Apache-2.0 Imports: 6 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(err *error, closer closerFunc, format string, a ...interface{})

Close runs function and on error return error by argument including the given error (usually from caller function).

func CloseWithLog

func CloseWithLog(logger Logger, closer closerFunc, format string, a ...interface{})

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.

Types

type Logger

type Logger interface {
	Log(keyvals ...interface{}) error
}

Logger interface compatible with go-kit/logger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL