|
| 1 | +/* |
| 2 | +Copyright The Kubernetes Authors. |
| 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 klog |
| 18 | + |
| 19 | +import "slices" |
| 20 | + |
| 21 | +// ErrorDetailer provides additional information about an error. |
| 22 | +// When an error value implements this additional interface, |
| 23 | +// the result of ErrorDetails will be logged in a separate key/value |
| 24 | +// pair. The result of Error is logged as usual. |
| 25 | +// |
| 26 | +// In Kubernetes, text and JSON output backends (aka klog and zapr) |
| 27 | +// will support this with "<error key>Details" (typically "errDetails") |
| 28 | +// as key for the additional value. |
| 29 | +// |
| 30 | +// Other backends might not support this, so all relevant information |
| 31 | +// should be in the error string. |
| 32 | +type ErrorDetailer interface { |
| 33 | + ErrorDetails() any |
| 34 | +} |
| 35 | + |
| 36 | +// ErrorWithDetails adds additional details to an error for logging. |
| 37 | +// If the base error already has such additional details, they |
| 38 | +// will be included in a list of details. |
| 39 | +// |
| 40 | +// A [PseudoStruct] can be used to log some key/value pairs as |
| 41 | +// if they were in a struct, without having to define such a struct. |
| 42 | +// The formatting may be nicer, too. |
| 43 | +func ErrorWithDetails(err error, details any) error { |
| 44 | + // This could be implemented as ErrorWithDetailsFunc(err, func() { return details }), |
| 45 | + // but having the details visible in the error instance may be more useful for |
| 46 | + // interactive debugging. |
| 47 | + return &errWithDetails{err, details} |
| 48 | +} |
| 49 | + |
| 50 | +type errWithDetails struct { |
| 51 | + error |
| 52 | + details any |
| 53 | +} |
| 54 | + |
| 55 | +var _ error = &errWithDetails{} |
| 56 | +var _ ErrorDetailer = &errWithDetails{} |
| 57 | + |
| 58 | +func (err *errWithDetails) ErrorDetails() any { |
| 59 | + if base, ok := err.error.(ErrorDetailer); ok { |
| 60 | + baseDetails := base.ErrorDetails() |
| 61 | + if baseDetailsList, ok := baseDetails.([]any); ok { |
| 62 | + // Flatten the list. |
| 63 | + return append(slices.Clone(baseDetailsList), err.details) |
| 64 | + } |
| 65 | + // Use a pair of values in a slice which gets detected above when nesting multiple times. |
| 66 | + return []any{baseDetails, err.details} |
| 67 | + } |
| 68 | + return err.details |
| 69 | +} |
| 70 | + |
| 71 | +// ErrorWithDetailsFunc adds additional details to an error for logging. |
| 72 | +// In contrast to [ErrorWithDetails], the additional details are provided |
| 73 | +// by the given function, which will be called only when needed. This |
| 74 | +// can be used to avoid building some potentially expensive data structure |
| 75 | +// that will not be needed when the error does not get logged. |
| 76 | +// |
| 77 | +// If the base error already has such additional details, they |
| 78 | +// will be included in a list of details. |
| 79 | +// |
| 80 | +// A [PseudoStruct] can be used to log some key/value pairs as |
| 81 | +// if they were in a struct, without having to define such a struct. |
| 82 | +// The formatting may be nicer, too. |
| 83 | +func ErrorWithDetailsFunc(err error, details func() any) error { |
| 84 | + return &errWithDetailsFunc{err, details} |
| 85 | +} |
| 86 | + |
| 87 | +type errWithDetailsFunc struct { |
| 88 | + error |
| 89 | + details func() any |
| 90 | +} |
| 91 | + |
| 92 | +var _ error = &errWithDetailsFunc{} |
| 93 | +var _ ErrorDetailer = &errWithDetailsFunc{} |
| 94 | + |
| 95 | +func (err *errWithDetailsFunc) ErrorDetails() any { |
| 96 | + if base, ok := err.error.(ErrorDetailer); ok { |
| 97 | + baseDetails := base.ErrorDetails() |
| 98 | + if baseDetailsList, ok := baseDetails.([]any); ok { |
| 99 | + // Flatten the list. |
| 100 | + return append(slices.Clone(baseDetailsList), err.details()) |
| 101 | + } |
| 102 | + // Use a pair of values in a slice which gets detected above when nesting multiple times. |
| 103 | + return []any{baseDetails, err.details()} |
| 104 | + } |
| 105 | + return err.details() |
| 106 | +} |
0 commit comments