OLD | NEW |
1 // This package provides an Error implementation which knows about types of erro
r, and which has support | 1 // This package provides an Error implementation which knows about types of erro
r, and which has support |
2 // for error causes. | 2 // for error causes. |
3 | 3 |
4 package errors | 4 package errors |
5 | 5 |
6 import "fmt" | 6 import "fmt" |
7 | 7 |
8 type Code string | 8 type Code string |
9 | 9 |
10 const ( | 10 const ( |
11 // Public available error types. | 11 // Public available error types. |
12 // These errors are provided because they are specifically required by b
usiness logic in the callers. | 12 // These errors are provided because they are specifically required by b
usiness logic in the callers. |
13 UnspecifiedError = Code("Unspecified") | 13 UnspecifiedError = Code("Unspecified") |
14 NotFoundError = Code("NotFound") | 14 NotFoundError = Code("NotFound") |
15 DuplicateValueError = Code("DuplicateValue") | 15 DuplicateValueError = Code("DuplicateValue") |
16 TimeoutError = Code("Timeout") | 16 TimeoutError = Code("Timeout") |
17 UnauthorisedError = Code("Unauthorised") | 17 UnauthorisedError = Code("Unauthorised") |
| 18 NotImplementedError = Code("NotImplemented") |
18 ) | 19 ) |
19 | 20 |
20 // Error instances store an optional error cause. | 21 // Error instances store an optional error cause. |
21 type Error interface { | 22 type Error interface { |
22 error | 23 error |
23 Cause() error | 24 Cause() error |
24 } | 25 } |
25 | 26 |
26 type gooseError struct { | 27 type gooseError struct { |
27 error | 28 error |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 return false | 89 return false |
89 } | 90 } |
90 | 91 |
91 func IsUnauthorised(err error) bool { | 92 func IsUnauthorised(err error) bool { |
92 if e, ok := err.(*gooseError); ok { | 93 if e, ok := err.(*gooseError); ok { |
93 return e.causedBy(UnauthorisedError) | 94 return e.causedBy(UnauthorisedError) |
94 } | 95 } |
95 return false | 96 return false |
96 } | 97 } |
97 | 98 |
98 // New creates a new Error instance with the specified cause. | 99 func IsNotImplemented(err error) bool { |
| 100 » if e, ok := err.(*gooseError); ok { |
| 101 » » return e.causedBy(NotImplementedError) |
| 102 » } |
| 103 » return false |
| 104 } |
| 105 |
| 106 // makeErrorf creates a new Error instance with the specified cause. |
99 func makeErrorf(code Code, cause error, format string, args ...interface{}) Erro
r { | 107 func makeErrorf(code Code, cause error, format string, args ...interface{}) Erro
r { |
100 return &gooseError{ | 108 return &gooseError{ |
101 errcode: code, | 109 errcode: code, |
102 error: fmt.Errorf(format, args...), | 110 error: fmt.Errorf(format, args...), |
103 cause: cause, | 111 cause: cause, |
104 } | 112 } |
105 } | 113 } |
106 | 114 |
107 // New creates a new Unspecified Error instance with the specified cause. | 115 // Newf creates a new Unspecified Error instance with the specified cause. |
108 func Newf(cause error, format string, args ...interface{}) Error { | 116 func Newf(cause error, format string, args ...interface{}) Error { |
109 return makeErrorf(UnspecifiedError, cause, format, args...) | 117 return makeErrorf(UnspecifiedError, cause, format, args...) |
110 } | 118 } |
111 | 119 |
112 // New creates a new NotFound Error instance with the specified cause. | 120 // NewNotFoundf creates a new NotFound Error instance with the specified cause. |
113 func NewNotFoundf(cause error, context interface{}, format string, args ...inter
face{}) Error { | 121 func NewNotFoundf(cause error, context interface{}, format string, args ...inter
face{}) Error { |
114 if format == "" { | 122 if format == "" { |
115 format = fmt.Sprintf("Not found: %s", context) | 123 format = fmt.Sprintf("Not found: %s", context) |
116 } | 124 } |
117 return makeErrorf(NotFoundError, cause, format, args...) | 125 return makeErrorf(NotFoundError, cause, format, args...) |
118 } | 126 } |
119 | 127 |
120 // New creates a new DuplicateValue Error instance with the specified cause. | 128 // NewDuplicateValuef creates a new DuplicateValue Error instance with the speci
fied cause. |
121 func NewDuplicateValuef(cause error, context interface{}, format string, args ..
.interface{}) Error { | 129 func NewDuplicateValuef(cause error, context interface{}, format string, args ..
.interface{}) Error { |
122 if format == "" { | 130 if format == "" { |
123 format = fmt.Sprintf("Duplicate: %s", context) | 131 format = fmt.Sprintf("Duplicate: %s", context) |
124 } | 132 } |
125 return makeErrorf(DuplicateValueError, cause, format, args...) | 133 return makeErrorf(DuplicateValueError, cause, format, args...) |
126 } | 134 } |
127 | 135 |
128 // New creates a new Timeout Error instance with the specified cause. | 136 // NewTimeoutf creates a new Timeout Error instance with the specified cause. |
129 func NewTimeoutf(cause error, context interface{}, format string, args ...interf
ace{}) Error { | 137 func NewTimeoutf(cause error, context interface{}, format string, args ...interf
ace{}) Error { |
130 if format == "" { | 138 if format == "" { |
131 format = fmt.Sprintf("Timeout: %s", context) | 139 format = fmt.Sprintf("Timeout: %s", context) |
132 } | 140 } |
133 return makeErrorf(TimeoutError, cause, format, args...) | 141 return makeErrorf(TimeoutError, cause, format, args...) |
134 } | 142 } |
135 | 143 |
136 // New creates a new Unauthorised Error instance with the specified cause. | 144 // NewUnauthorisedf creates a new Unauthorised Error instance with the specified
cause. |
137 func NewUnauthorisedf(cause error, context interface{}, format string, args ...i
nterface{}) Error { | 145 func NewUnauthorisedf(cause error, context interface{}, format string, args ...i
nterface{}) Error { |
138 if format == "" { | 146 if format == "" { |
139 format = fmt.Sprintf("Unauthorised: %s", context) | 147 format = fmt.Sprintf("Unauthorised: %s", context) |
140 } | 148 } |
141 return makeErrorf(UnauthorisedError, cause, format, args...) | 149 return makeErrorf(UnauthorisedError, cause, format, args...) |
142 } | 150 } |
| 151 |
| 152 // NewNotImplementedf creates a new NotImplemented Error instance with the speci
fied cause. |
| 153 func NewNotImplementedf(cause error, context interface{}, format string, args ..
.interface{}) Error { |
| 154 if format == "" { |
| 155 format = fmt.Sprintf("Not implemented: %s", context) |
| 156 } |
| 157 return makeErrorf(NotImplementedError, cause, format, args...) |
| 158 } |
OLD | NEW |