forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
80 lines (65 loc) · 1.32 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package errors
import (
"bytes"
"fmt"
"io"
"runtime/debug"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/cmd/util/prefixwriter"
)
type Error interface {
error
WithCause(error) Error
WithSolution(string) Error
WithDetails(string) Error
}
func NewError(msg string, args ...interface{}) Error {
return &internalError{
msg: fmt.Sprintf(msg, args...),
}
}
type internalError struct {
msg string
cause error
solution string
details string
}
func (e *internalError) Error() string {
return e.msg
}
func (e *internalError) Cause() error {
return e.cause
}
func (e *internalError) Solution() string {
return e.solution
}
func (e *internalError) Details() string {
return e.details
}
func (e *internalError) WithCause(err error) Error {
e.cause = err
return e
}
func (e *internalError) WithDetails(details string) Error {
e.details = details
return e
}
func (e *internalError) WithSolution(solution string) Error {
e.solution = fmt.Sprintf(solution)
return e
}
func LogError(err error) {
if err == nil {
return
}
glog.V(1).Infof("Unexpected error: %v", err)
if glog.V(5) {
debug.PrintStack()
}
}
func PrintLog(out io.Writer, title string, content []byte) {
fmt.Fprintf(out, "%s:\n", title)
w := prefixwriter.New(" ", out)
w.Write(bytes.TrimSpace(content))
fmt.Fprintf(out, "\n")
}