blob: 0ec10893a6d8baccac2c908256d1dbf5991e1921 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
2 * Copyright 2020-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//Package olterrors implements functions to manipulate OLT errors
18package olterrors
19
20import (
21 "context"
22 "encoding/json"
23 "fmt"
24 "github.com/opencord/voltha-lib-go/v3/pkg/log"
25 "strings"
26)
27
28const (
29 defaultLogAndReturnLevel = log.ErrorLevel
30)
31
32func copy(src log.Fields) log.Fields {
33 dst := make(log.Fields)
34 for k, v := range src {
35 dst[k] = v
36 }
37 return dst
38}
39
40func merge(one, two log.Fields) log.Fields {
41 dst := make(log.Fields)
42 for k, v := range one {
43 dst[k] = v
44 }
45 for k, v := range two {
46 dst[k] = v
47 }
48 return dst
49}
50
51// LoggableError defined functions that can be used to log an object
52type LoggableError interface {
53 error
54 Log() error
55 LogAt(log.LogLevel) error
56}
57
58// ErrAdapter represents a basic adapter error that combines an name, field set
59// and wrapped error
60type ErrAdapter struct {
61 name string
62 fields log.Fields
63 wrapped error
64}
65
66// NewErrAdapter constructs a new error with the given values
67func NewErrAdapter(name string, fields log.Fields, wrapped error) LoggableError {
68 return &ErrAdapter{
69 name: name,
70 fields: copy(fields),
71 wrapped: wrapped,
72 }
73}
74
75// Name returns the error name
76func (e *ErrAdapter) Name() string {
77 return e.name
78}
79
80// Fields returns the fields associated with the error
81func (e *ErrAdapter) Fields() log.Fields {
82 return e.fields
83}
84
85// Unwrap returns the wrapped or nested error
86func (e *ErrAdapter) Unwrap() error {
87 return e.wrapped
88}
89
90// Error returns a string representation of the error
91func (e *ErrAdapter) Error() string {
92 ctx := context.Background()
93 var buf strings.Builder
94 _, er := buf.WriteString(e.name)
95 if er != nil {
96 logger.Error(ctx, er)
97 }
98 if len(e.fields) > 0 {
99 if val, err := json.Marshal(e.fields); err == nil {
100 _, er = buf.WriteString(fmt.Sprintf(": [%s]", string(val)))
101 if er != nil {
102 logger.Error(ctx, er)
103 }
104 }
105 }
106 if e.wrapped != nil {
107 _, er = buf.WriteString(fmt.Sprintf(": %s", e.wrapped.Error()))
108 if er != nil {
109 logger.Error(ctx, er)
110 }
111 }
112 return buf.String()
113}
114
115// Log logs the error at the default level for log and return
116func (e *ErrAdapter) Log() error {
117 return e.LogAt(defaultLogAndReturnLevel)
118}
119
120// LogAt logs the error at the specified level and then returns the error
121func (e *ErrAdapter) LogAt(level log.LogLevel) error {
122 loggerFunc := logger.Debugw
123 switch level {
124 case log.InfoLevel:
125 loggerFunc = logger.Infow
126 case log.WarnLevel:
127 loggerFunc = logger.Warnw
128 case log.ErrorLevel:
129 loggerFunc = logger.Errorw
130 case log.FatalLevel:
131 loggerFunc = logger.Fatalw
132 }
133 local := e.fields
134 if e.wrapped != nil {
135 local = merge(e.fields, log.Fields{"wrapped": e.wrapped})
136 }
137 loggerFunc(context.Background(), e.name, local)
138 return e
139}
140
141// ErrInvalidValue represents an error condition with given value is not able to
142// be processed
143type ErrInvalidValue struct {
144 ErrAdapter
145}
146
147// NewErrInvalidValue constructs a new error based on the given values
148func NewErrInvalidValue(fields log.Fields, wrapped error) LoggableError {
149 return &ErrInvalidValue{
150 ErrAdapter{
151 name: "invalid-value",
152 fields: copy(fields),
153 wrapped: wrapped,
154 },
155 }
156}
157
158// Log logs the error at the default level for log and return
159func (e *ErrInvalidValue) Log() error {
160 _ = e.ErrAdapter.Log()
161 return e
162}
163
164// LogAt logs the error at the specified level and then returns the error
165func (e *ErrInvalidValue) LogAt(level log.LogLevel) error {
166 _ = e.ErrAdapter.LogAt(level)
167 return e
168}
169
170// ErrNotFound represents an error condition when a value can not be located
171// given a field set of criteria
172type ErrNotFound struct {
173 ErrAdapter
174}
175
176// NewErrNotFound constructs a new error based on the given values
177func NewErrNotFound(target string, fields log.Fields, wrapped error) LoggableError {
178 return &ErrNotFound{
179 ErrAdapter{
180 name: "not-found",
181 fields: merge(fields, log.Fields{"target": target}),
182 wrapped: wrapped,
183 },
184 }
185}
186
187// Log logs the error at the default level for log and return
188func (e *ErrNotFound) Log() error {
189 _ = e.ErrAdapter.Log()
190 return e
191}
192
193// LogAt logs the error at the specified level and then returns the error
194func (e *ErrNotFound) LogAt(level log.LogLevel) error {
195 _ = e.ErrAdapter.LogAt(level)
196 return e
197}
198
199// ErrPersistence representation an error condition when a persistence operation
200// did not succeed
201type ErrPersistence struct {
202 ErrAdapter
203}
204
205// NewErrPersistence constructs a new error based on the given values
206func NewErrPersistence(operation, entityType string, ID uint32, fields log.Fields, wrapped error) LoggableError {
207 return &ErrPersistence{
208 ErrAdapter{
209 name: "unable-to-persist",
210 fields: merge(fields, log.Fields{
211 "operation": operation,
212 "entity-type": entityType,
213 "id": fmt.Sprintf("0x%x", ID)}),
214 wrapped: wrapped,
215 },
216 }
217}
218
219// Log logs the error at the default level for log and return
220func (e *ErrPersistence) Log() error {
221 _ = e.ErrAdapter.Log()
222 return e
223}
224
225// LogAt logs the error at the specified level and then returns the error
226func (e *ErrPersistence) LogAt(level log.LogLevel) error {
227 _ = e.ErrAdapter.LogAt(level)
228 return e
229}
230
231// ErrCommunication representation an error condition when an interprocess
232// message communication fails
233type ErrCommunication struct {
234 ErrAdapter
235}
236
237// NewErrCommunication constructs a new error based on the given values
238func NewErrCommunication(operation string, fields log.Fields, wrapped error) LoggableError {
239 return &ErrCommunication{
240 ErrAdapter{
241 name: "failed-communication",
242 fields: merge(fields, log.Fields{
243 "operation": operation}),
244 wrapped: wrapped,
245 },
246 }
247}
248
249// Log logs the error at the default level for log and return
250func (e *ErrCommunication) Log() error {
251 _ = e.ErrAdapter.Log()
252 return e
253}
254
255// LogAt logs the error at the specified level and then returns the error
256func (e *ErrCommunication) LogAt(level log.LogLevel) error {
257 _ = e.ErrAdapter.LogAt(level)
258 return e
259}
260
261// ErrFlowOp represents an error condition when a flow operation to a device did
262// not succeed
263type ErrFlowOp struct {
264 ErrAdapter
265}
266
267// NewErrFlowOp constructs a new error based on the given values
268func NewErrFlowOp(operation string, ID uint32, fields log.Fields, wrapped error) LoggableError {
269 return &ErrPersistence{
270 ErrAdapter{
271 name: "unable-to-perform-flow-operation",
272 fields: merge(fields, log.Fields{
273 "operation": operation,
274 "id": fmt.Sprintf("0x%x", ID)}),
275 wrapped: wrapped,
276 },
277 }
278}
279
280// Log logs the error at the default level for log and return
281func (e *ErrFlowOp) Log() error {
282 _ = e.ErrAdapter.Log()
283 return e
284}
285
286// LogAt logs the error at the specified level and then returns the error
287func (e *ErrFlowOp) LogAt(level log.LogLevel) error {
288 _ = e.ErrAdapter.LogAt(level)
289 return e
290}
291
292// ErrGroupOp represents an error condition when a group operation to a device did
293// not succeed
294type ErrGroupOp struct {
295 ErrAdapter
296}
297
298// NewErrGroupOp constructs a new error based on the given values
299func NewErrGroupOp(operation string, ID uint32, fields log.Fields, wrapped error) LoggableError {
300 return &ErrPersistence{
301 ErrAdapter{
302 name: "unable-to-perform-group-operation",
303 fields: merge(fields, log.Fields{
304 "operation": operation,
305 "id": fmt.Sprintf("0x%x", ID)}),
306 wrapped: wrapped,
307 },
308 }
309}
310
311// Log logs the error at the default level for log and return
312func (e *ErrGroupOp) Log() error {
313 _ = e.ErrAdapter.Log()
314 return e
315}
316
317// LogAt logs the error at the specified level and then returns the error
318func (e *ErrGroupOp) LogAt(level log.LogLevel) error {
319 _ = e.ErrAdapter.LogAt(level)
320 return e
321}
322
323// ErrTimeout represents an error condition when the deadline for performing an
324// operation has been exceeded
325type ErrTimeout struct {
326 ErrAdapter
327}
328
329// NewErrTimeout constructs a new error based on the given values
330func NewErrTimeout(operation string, fields log.Fields, wrapped error) LoggableError {
331 return &ErrTimeout{
332 ErrAdapter{
333 name: "operation-timed-out",
334 fields: merge(fields, log.Fields{"operation": operation}),
335 wrapped: wrapped,
336 },
337 }
338}
339
340// Log logs the error at the default level for log and return
341func (e *ErrTimeout) Log() error {
342 _ = e.ErrAdapter.Log()
343 return e
344}
345
346// LogAt logs the error at the specified level and then returns the error
347func (e *ErrTimeout) LogAt(level log.LogLevel) error {
348 _ = e.ErrAdapter.LogAt(level)
349 return e
350}
351
352var (
353 ErrNotImplemented = NewErrAdapter("not-implemented", nil, nil)
354
355 ErrInvalidPortRange = NewErrAdapter("invalid-port-range", nil, nil)
356
357 ErrStateTransition = NewErrAdapter("state-transition", nil, nil)
358
359 ErrResourceManagerInstantiating = NewErrAdapter("resoure-manager-instantiating", nil, nil)
360)