| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | // Initially copied from Thanos |
| 5 | // |
| 6 | // Copyright (c) The Thanos Authors. |
| 7 | // Licensed under the Apache License 2.0. |
| 8 | |
| 9 | package errcapturescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errcapture`/ |
| 10 | |
| 11 | import ( |
| 12 | "ioscip-go gomod github.com/golang/go/src go1.17 io/" |
| 13 | "osscip-go gomod github.com/golang/go/src go1.17 os/" |
| 14 | |
| 15 | "github.com/efficientgo/core/errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/" |
| 16 | "github.com/efficientgo/core/merrorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/" |
| 17 | ) |
| 18 | |
| 19 | type doFuncscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errcapture`/doFunc# func() error |
| 20 | |
| 21 | // Do runs function and on error return error by argument including the given error (usually |
| 22 | // from caller function). |
| 23 | func Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errcapture`/Do().(err *error, doer doFuncscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errcapture`/doFunc#, format string, a ...interface{}) { |
| 24 | derr := doer() |
| 25 | if err == nil { |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | // For os closers, it's a common case to double close. From reliability purpose this is not a problem it may only indicate |
| 30 | // surprising execution path. |
| 31 | if errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Isscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Is().(derr, osscip-go gomod github.com/golang/go/src go1.17 os/.ErrClosedscip-go gomod github.com/golang/go/src go1.17 os/ErrClosed.) { |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | *err = merrorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/.Newscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/New().(*err, errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Wrapfscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Wrapf().(derr, format, a...)).Errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/NilOrMultiError#Err().() |
| 36 | } |
| 37 | |
| 38 | // ExhaustClose closes the io.ReadCloser with error capture but exhausts the reader before. |
| 39 | func ExhaustClosescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errcapture`/ExhaustClose().(err *error, r ioscip-go gomod github.com/golang/go/src go1.17 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.17 io/ReadCloser#, format string, a ...interface{}) { |
| 40 | _, copyErr := ioscip-go gomod github.com/golang/go/src go1.17 io/.Copyscip-go gomod github.com/golang/go/src go1.17 io/Copy().(ioscip-go gomod github.com/golang/go/src go1.17 io/.Discardscip-go gomod github.com/golang/go/src go1.17 io/Discard., r) |
| 41 | |
| 42 | Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errcapture`/Do().(err, r.Closescip-go gomod github.com/golang/go/src go1.17 io/Closer#Close()., format, a...) |
| 43 | |
| 44 | // Prepend the io.Copy error. |
| 45 | *err = merrorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/.Newscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/New().(copyErr, *err).Errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/merrors`/NilOrMultiError#Err().() |
| 46 | } |