diff options
| author | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-05-23 18:32:18 -0700 |
|---|---|---|
| committer | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-05-31 12:33:58 -0700 |
| commit | 4b6d0ce14b8553a93b2d14fd858dfd35bfd61104 (patch) | |
| tree | 39dd221bd5f7118537ae33c43ea23e4d4af65c86 /util/util.go | |
| parent | bd6a1acc8c6b6c03f999558baa4aab464417548d (diff) | |
util: better handing of custom errors
This commit changes how we handle InvalidInput and System errors.
Instead of having formatting functions, the now just wrap a string and
should be created with fmt.Sprintf or similar. We also move all of the
error related code into a single place.
Finally, the utils package gets additional functionality with MinInt64
and GetUsername, and the UnderlyingError function gets better logging.
Note that this will break packages that depend on it. For instance,
metadata and crypto currently do not build. This is fixed in a later
commit.
Change-Id: I819e4d1970604456a5b4b6a7c86426f180a6d092
Diffstat (limited to 'util/util.go')
| -rw-r--r-- | util/util.go | 74 |
1 files changed, 11 insertions, 63 deletions
diff --git a/util/util.go b/util/util.go index dc1b85d..2f20151 100644 --- a/util/util.go +++ b/util/util.go @@ -24,74 +24,14 @@ package util import ( - "io" "unsafe" ) -// ErrReader wraps an io.Reader, passing along calls to Read() until a read -// fails. Then, the error is stored, and all subsequent calls to Read() do -// nothing. This allows you to write code which has many subsequent reads and -// do all of the error checking at the end. For example: -// -// r := NewErrReader(reader) -// r.Read(foo) -// io.ReadFull(r, bar) -// if r.Err() != nil { -// // Handle error -// } -// -// Taken from https://blog.golang.org/errors-are-values by Rob Pike. -type ErrReader struct { - r io.Reader - err error -} - -// NewErrReader creates an ErrReader which wraps the provided reader. -func NewErrReader(reader io.Reader) *ErrReader { - return &ErrReader{r: reader, err: nil} -} - -// Read runs ReadFull on the wrapped reader if no errors have occurred. -// Otherwise, the previous error is just returned and no reads are attempted. -func (e *ErrReader) Read(p []byte) (n int, err error) { - if e.err == nil { - n, e.err = io.ReadFull(e.r, p) - } - return n, e.err -} - -// Err returns the first encountered err (or nil if no errors occurred). -func (e *ErrReader) Err() error { - return e.err -} - -// ErrWriter works exactly like ErrReader, except with io.Writer. -type ErrWriter struct { - w io.Writer - err error -} - -// NewErrWriter creates an ErrWriter which wraps the provided reader. -func NewErrWriter(writer io.Writer) *ErrWriter { - return &ErrWriter{w: writer, err: nil} -} - -// Write runs the wrapped writer's Write if no errors have occurred. Otherwise, -// the previous error is just returned and no writes are attempted. -func (e *ErrWriter) Write(p []byte) (n int, err error) { - if e.err == nil { - n, e.err = e.w.Write(p) - } - return n, e.err -} - -// Err returns the first encountered err (or nil if no errors occurred). -func (e *ErrWriter) Err() error { - return e.err -} - // Ptr converts an Go byte array to a pointer to the start of the array. func Ptr(slice []byte) unsafe.Pointer { + if len(slice) == 0 { + return nil + } return unsafe.Pointer(&slice[0]) } @@ -124,3 +64,11 @@ func MinInt(a, b int) int { } return b } + +// MinInt64 returns the lesser of a and b. +func MinInt64(a, b int64) int64 { + if a < b { + return a + } + return b +} |