forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
65 lines (52 loc) · 1.23 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package errors
import (
"errors"
"fmt"
)
type ShortenableError interface {
error
ShortError() string
}
type ComplexError struct {
Err error
Cause error
}
func (e ComplexError) Error() string {
return fmt.Sprintf("%s: %s", e.Err.Error(), e.Cause.Error())
}
func (e ComplexError) ShortError() string {
var errorMessage string
if shortenableError, ok := e.Err.(ShortenableError); ok {
errorMessage = shortenableError.ShortError()
} else {
errorMessage = e.Err.Error()
}
var causeMessage string
if shortenableCause, ok := e.Cause.(ShortenableError); ok {
causeMessage = shortenableCause.ShortError()
} else {
causeMessage = e.Cause.Error()
}
return fmt.Sprintf("%s: %s", errorMessage, causeMessage)
}
func Error(msg string) error {
return errors.New(msg)
}
func Errorf(msg string, args ...interface{}) error {
return fmt.Errorf(msg, args...)
}
func WrapError(cause error, msg string) error {
return WrapComplexError(cause, Error(msg))
}
func WrapErrorf(cause error, msg string, args ...interface{}) error {
return WrapComplexError(cause, Errorf(msg, args...))
}
func WrapComplexError(cause, err error) error {
if cause == nil {
cause = Error("<nil cause>")
}
return ComplexError{
Err: err,
Cause: cause,
}
}