forked from juju/errors
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrortypes.go
More file actions
178 lines (151 loc) · 4.74 KB
/
errortypes.go
File metadata and controls
178 lines (151 loc) · 4.74 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package errgo
import (
"fmt"
"net/http"
)
// wrap is a helper to construct an *wrapper.
func wrap(err error, code int, format, suffix string, args ...interface{}) Err {
newErr := Err{
message: fmt.Sprintf(format+suffix, args...),
previous: err,
}
newErr.SetLocation(2)
newErr.SetCode(code)
return newErr
}
// InternalServer represents an error when something unexpected has happened.
// InternalServerf returns an error which satisfies IsInternalServer().
func InternalServerf(format string, args ...interface{}) error {
e := wrap(nil, http.StatusInternalServerError, format, "", args...)
return &e
}
// NewInternalServer returns an error which wraps err that satisfies
// IsInternalServer().
func NewInternalServer(err error, msg string) error {
e := wrap(err, http.StatusInternalServerError, msg, "")
return &e
}
// IsInternalServer reports whether err was created with InternalServerf() or
// NewInternalServer().
func IsInternalServer(err error) bool {
err = Cause(err)
e, ok := err.(*Err)
if ok {
return e.Code() == http.StatusInternalServerError
}
return ok
}
// NotFound represents an error when something has not been found.
// NotFoundf returns an error which satisfies IsNotFound().
func NotFoundf(format string, args ...interface{}) error {
e := wrap(nil, http.StatusNotFound, format, "", args...)
return &e
}
// NewNotFound returns an error which wraps err that satisfies
// IsNotFound().
func NewNotFound(err error, msg string) error {
e := wrap(err, http.StatusNotFound, msg, "")
return &e
}
// IsNotFound reports whether err was created with NotFoundf() or
// NewNotFound().
func IsNotFound(err error) bool {
err = Cause(err)
e, ok := err.(*Err)
if ok {
return e.Code() == http.StatusNotFound
}
return ok
}
// Unauthorized represents an error when an operation is unauthorized.
// Unauthorizedf returns an error which satisfies IsUnauthorized().
func Unauthorizedf(format string, args ...interface{}) error {
e := wrap(nil, http.StatusUnauthorized, format, "", args...)
return &e
}
// NewUnauthorized returns an error which wraps err and satisfies
// IsUnauthorized().
func NewUnauthorized(err error, msg string) error {
e := wrap(err, http.StatusUnauthorized, msg, "")
return &e
}
// IsUnauthorized reports whether err was created with Unauthorizedf() or
// NewUnauthorized().
func IsUnauthorized(err error) bool {
err = Cause(err)
e, ok := err.(*Err)
if ok {
return e.Code() == http.StatusUnauthorized
}
return ok
}
// NotImplemented represents an error when something is not
// implemented.
// NotImplementedf returns an error which satisfies IsNotImplemented().
func NotImplementedf(format string, args ...interface{}) error {
e := wrap(nil, http.StatusNotImplemented, format, "", args...)
return &e
}
// NewNotImplemented returns an error which wraps err and satisfies
// IsNotImplemented().
func NewNotImplemented(err error, msg string) error {
e := wrap(err, http.StatusNotImplemented, msg, "")
return &e
}
// IsNotImplemented reports whether err was created with
// NotImplementedf() or NewNotImplemented().
func IsNotImplemented(err error) bool {
err = Cause(err)
e, ok := err.(*Err)
if ok {
return e.Code() == http.StatusNotImplemented
}
return ok
}
// BadRequest represents an error when a request has bad parameters
// BadRequestf returns an error which satisfies IsBadRequest().
func BadRequestf(format string, args ...interface{}) error {
e := wrap(nil, http.StatusBadRequest, format, "", args...)
return &e
}
// NewBadRequest returns an error which wraps err that satisfies
// IsBadRequest().
func NewBadRequest(err error, msg string) error {
e := wrap(err, http.StatusBadRequest, msg, "")
return &e
}
// IsBadRequest reports whether err was created with BadRequestf() or
// NewBadRequest().
func IsBadRequest(err error) bool {
err = Cause(err)
e, ok := err.(*Err)
if ok {
return e.Code() == http.StatusBadRequest
}
return ok
}
// MethodNotAllowed represents an error when an HTTP request
// is made with an inappropriate method.
// MethodNotAllowedf returns an error which satisfies IsMethodNotAllowed().
func MethodNotAllowedf(format string, args ...interface{}) error {
e := wrap(nil, http.StatusMethodNotAllowed, format, "", args...)
return &e
}
// NewMethodNotAllowed returns an error which wraps err that satisfies
// IsMethodNotAllowed().
func NewMethodNotAllowed(err error, msg string) error {
e := wrap(err, http.StatusMethodNotAllowed, msg, "")
return &e
}
// IsMethodNotAllowed reports whether err was created with MethodNotAllowedf() or
// NewMethodNotAllowed().
func IsMethodNotAllowed(err error) bool {
err = Cause(err)
e, ok := err.(*Err)
if ok {
return e.Code() == http.StatusMethodNotAllowed
}
return ok
}