-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
errors.go
50 lines (41 loc) · 1.25 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Package errors provides libraries for working with the go-errors/errors library.
package errors
import (
"fmt"
goerrors "github.com/go-errors/errors"
)
// Wrap returns err wrapped in a go-error. If err is nil, returns nil.
func Wrap(err interface{}) error {
if err == nil {
return nil
}
return goerrors.Wrap(err, 1)
}
// WrapPrefixf returns err wrapped in a go-error with a message prefix. If err is nil, returns nil.
func WrapPrefixf(err interface{}, msg string, args ...interface{}) error {
if err == nil {
return nil
}
return goerrors.WrapPrefix(err, fmt.Sprintf(msg, args...), 1)
}
// Errorf returns a new go-error.
func Errorf(msg string, args ...interface{}) error {
return goerrors.Wrap(fmt.Errorf(msg, args...), 1)
}
// As finds the targeted error in any wrapped error.
func As(err error, target interface{}) bool {
return goerrors.As(err, target)
}
// Is detects whether the error is equal to a given error.
func Is(err error, target error) bool {
return goerrors.Is(err, target)
}
// GetStack returns a stack trace for the error if it has one
func GetStack(err error) string {
if e, ok := err.(*goerrors.Error); ok {
return string(e.Stack())
}
return ""
}