forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathjuju_adaptor.go
154 lines (135 loc) · 3.52 KB
/
juju_adaptor.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package errors
import (
"fmt"
"strings"
)
// ==================== juju adaptor start ========================
// Trace just calls AddStack.
func Trace(err error) error {
if err == nil {
return nil
}
return AddStack(err)
}
// Annotate adds a message and ensures there is a stack trace.
func Annotate(err error, message string) error {
if err == nil {
return nil
}
hasStack := HasStack(err)
err = &withMessage{
cause: err,
msg: message,
causeHasStack: hasStack,
}
if hasStack {
return err
}
return &withStack{
err,
callers(),
}
}
// Annotatef adds a message and ensures there is a stack trace.
func Annotatef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
hasStack := HasStack(err)
err = &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
causeHasStack: hasStack,
}
if hasStack {
return err
}
return &withStack{
err,
callers(),
}
}
var emptyStack stack
// NewNoStackError creates error without error stack
// later duplicate trace will no longer generate Stack too.
func NewNoStackError(msg string) error {
return &fundamental{
msg: msg,
stack: &emptyStack,
}
}
// NewNoStackErrorf creates error with error stack and formats according
// to a format specifier and returns the string as a value that satisfies error.
func NewNoStackErrorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
stack: &emptyStack,
}
}
// SuspendStack suspends stack generate for error.
func SuspendStack(err error) error {
if err == nil {
return err
}
cleared := clearStack(err)
if cleared {
return err
}
return &withStack{
err,
&emptyStack,
}
}
func clearStack(err error) (cleared bool) {
switch typedErr := err.(type) {
case *withMessage:
return clearStack(typedErr.Cause())
case *fundamental:
typedErr.stack = &emptyStack
return true
case *withStack:
typedErr.stack = &emptyStack
clearStack(typedErr.Cause())
return true
default:
return false
}
}
// ErrorStack will format a stack trace if it is available, otherwise it will be Error()
// If the error is nil, the empty string is returned
// Note that this just calls fmt.Sprintf("%+v", err)
func ErrorStack(err error) string {
if err == nil {
return ""
}
return fmt.Sprintf("%+v", err)
}
// IsNotFound reports whether err was not found error.
func IsNotFound(err error) bool {
return strings.Contains(err.Error(), "not found")
}
// NotFoundf represents an error with not found message.
func NotFoundf(format string, args ...interface{}) error {
return Errorf(format+" not found", args...)
}
// BadRequestf represents an error with bad request message.
func BadRequestf(format string, args ...interface{}) error {
return Errorf(format+" bad request", args...)
}
// NotSupportedf represents an error with not supported message.
func NotSupportedf(format string, args ...interface{}) error {
return Errorf(format+" not supported", args...)
}
// NotValidf represents an error with not valid message.
func NotValidf(format string, args ...interface{}) error {
return Errorf(format+" not valid", args...)
}
// IsAlreadyExists reports whether err was already exists error.
func IsAlreadyExists(err error) bool {
return strings.Contains(err.Error(), "already exists")
}
// AlreadyExistsf represents an error with already exists message.
func AlreadyExistsf(format string, args ...interface{}) error {
return Errorf(format+" already exists", args...)
}
// ==================== juju adaptor end ========================