|
| 1 | +// Copyright 2019 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | + |
| 15 | +package barriers |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/cockroachdb/errors/errbase" |
| 21 | + "github.com/gogo/protobuf/proto" |
| 22 | +) |
| 23 | + |
| 24 | +// Handled swallows the provided error and hides is from the |
| 25 | +// Cause()/Unwrap() interface, and thus the Is() facility that |
| 26 | +// identifies causes. However, it retains it for the purpose of |
| 27 | +// printing the error out (e.g. for troubleshooting). The error |
| 28 | +// message is preserved in full. |
| 29 | +func Handled(err error) error { |
| 30 | + if err == nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + return HandledWithMessage(err, err.Error()) |
| 34 | +} |
| 35 | + |
| 36 | +// HandledWithMessage is like Handled except the message is overridden. |
| 37 | +// This can be used e.g. to hide message details or to prevent |
| 38 | +// downstream code to make assertions on the message's contents. |
| 39 | +func HandledWithMessage(err error, msg string) error { |
| 40 | + if err == nil { |
| 41 | + return nil |
| 42 | + } |
| 43 | + return &barrierError{maskedErr: err, msg: msg} |
| 44 | +} |
| 45 | + |
| 46 | +// HandledWithMessagef is like HandledWithMessagef except the message |
| 47 | +// is formatted. |
| 48 | +func HandledWithMessagef(err error, format string, args ...interface{}) error { |
| 49 | + if err == nil { |
| 50 | + return nil |
| 51 | + } |
| 52 | + return &barrierError{maskedErr: err, msg: fmt.Sprintf(format, args...)} |
| 53 | +} |
| 54 | + |
| 55 | +// barrierError is a leaf error type. It encapsulates a chain of |
| 56 | +// original causes, but these causes are hidden so that they inhibit |
| 57 | +// matching via Is() and the Cause()/Unwrap() recursions. |
| 58 | +type barrierError struct { |
| 59 | + // Message for the barrier itself. |
| 60 | + // In the common case, the message from the masked error |
| 61 | + // is used as-is (see Handled() above) however it is |
| 62 | + // useful to cache it here since the masked error may |
| 63 | + // have a long chain of wrappers and its Error() call |
| 64 | + // may be expensive. |
| 65 | + msg string |
| 66 | + // Masked error chain. |
| 67 | + maskedErr error |
| 68 | +} |
| 69 | + |
| 70 | +var _ error = &barrierError{} |
| 71 | +var _ errbase.SafeDetailer = &barrierError{} |
| 72 | + |
| 73 | +// barrierError is an error. |
| 74 | +func (e *barrierError) Error() string { return e.msg } |
| 75 | + |
| 76 | +// SafeDetails reports the PII-free details from the masked error. |
| 77 | +func (e *barrierError) SafeDetails() []string { |
| 78 | + var details []string |
| 79 | + for err := e.maskedErr; err != nil; err = errbase.UnwrapOnce(err) { |
| 80 | + sd := errbase.GetSafeDetails(err) |
| 81 | + details = sd.Fill(details) |
| 82 | + } |
| 83 | + return details |
| 84 | +} |
| 85 | + |
| 86 | +// Printing a barrier reveals the details. |
| 87 | +func (e *barrierError) Format(s fmt.State, verb rune) { |
| 88 | + switch verb { |
| 89 | + case 'v': |
| 90 | + if s.Flag('+') { |
| 91 | + fmt.Fprintf(s, "%s", e.msg) |
| 92 | + if e.maskedErr != nil { |
| 93 | + fmt.Fprintf(s, "\n-- original cause:\n") |
| 94 | + errbase.FormatError(s, verb, e.maskedErr) |
| 95 | + } |
| 96 | + return |
| 97 | + } |
| 98 | + fallthrough |
| 99 | + case 's', 'q': |
| 100 | + fmt.Fprintf(s, fmt.Sprintf("%%%c", verb), e.msg) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// A barrier error is encoded exactly. |
| 105 | +func encodeBarrier(err error) (msg string, details []string, payload proto.Message) { |
| 106 | + e := err.(*barrierError) |
| 107 | + enc := errbase.EncodeError(e.maskedErr) |
| 108 | + return e.msg, e.SafeDetails(), &enc |
| 109 | +} |
| 110 | + |
| 111 | +// A barrier error is decoded exactly. |
| 112 | +func decodeBarrier(msg string, _ []string, payload proto.Message) error { |
| 113 | + enc := payload.(*errbase.EncodedError) |
| 114 | + return &barrierError{msg: msg, maskedErr: errbase.DecodeError(*enc)} |
| 115 | +} |
| 116 | + |
| 117 | +func init() { |
| 118 | + tn := errbase.GetTypeKey(&barrierError{}) |
| 119 | + errbase.RegisterLeafDecoder(tn, decodeBarrier) |
| 120 | + errbase.RegisterLeafEncoder(tn, encodeBarrier) |
| 121 | +} |
0 commit comments