github.com/cockroachdb/errors@v1.11.1/errbase/formatter.go (about)

     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  // This file is taken from golang.org/x/xerrors,
    16  // at commit 3ee3066db522c6628d440a3a91c4abdd7f5ef22f (2019-05-10).
    17  // From the original code:
    18  // Copyright 2018 The Go Authors. All rights reserved.
    19  // Use of this source code is governed by a BSD-style
    20  // license that can be found in the LICENSE file.
    21  
    22  package errbase
    23  
    24  // A Formatter formats error messages.
    25  //
    26  // NB: Consider implementing SafeFormatter instead. This will ensure
    27  // that error displays can distinguish bits that are PII-safe.
    28  type Formatter interface {
    29  	error
    30  
    31  	// FormatError prints the receiver's first error.
    32  	// The return value decides what happens in the case
    33  	// FormatError() is used to produce a "short" message,
    34  	// eg. when it is used to implement Error():
    35  	//
    36  	// - if it returns nil, then the short message
    37  	//   contains no more than that produced for this error,
    38  	//   even if the error has a further causal chain.
    39  	//
    40  	// - if it returns non-nil, then the short message
    41  	//   contains the value printed by this error,
    42  	//   followed by that of its causal chain.
    43  	//   (e.g. thiserror: itscause: furthercause)
    44  	//
    45  	// Note that all the causal chain is reported in verbose reports in
    46  	// any case.
    47  	FormatError(p Printer) (next error)
    48  }
    49  
    50  // SafeFormatter is implemented by error leaf or wrapper types that want
    51  // to separate safe and non-safe information when printed out.
    52  //
    53  // When multiple errors are chained (e.g. via errors.Wrap), intermediate
    54  // layers in the error that do not implement SafeError are considered
    55  // “unsafe”
    56  type SafeFormatter interface {
    57  	// SafeFormatError prints the receiver's first error.
    58  	//
    59  	// The provided Printer behaves like a redact.SafePrinter its
    60  	// Print() and Printf() methods conditionally add redaction markers
    61  	// around unsafe bits.
    62  	//
    63  	// The return value of SafeFormatError() decides what happens in the
    64  	// case the method is used to produce a "short" message, eg. when it
    65  	// is used to implement Error():
    66  	//
    67  	// - if it returns nil, then the short message
    68  	//   contains no more than that produced for this error,
    69  	//   even if the error has a further causal chain.
    70  	//
    71  	// - if it returns non-nil, then the short message
    72  	//   contains the value printed by this error,
    73  	//   followed by that of its causal chain.
    74  	//   (e.g. thiserror: itscause: furthercause)
    75  	//
    76  	// Note that all the causal chain is reported in verbose reports in
    77  	// any case.
    78  	SafeFormatError(p Printer) (next error)
    79  }
    80  
    81  // A Printer formats error messages.
    82  //
    83  // The most common implementation of Printer is the one provided by package fmt
    84  // during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message
    85  // typically provide their own implementations.
    86  type Printer interface {
    87  	// Print appends args to the message output.
    88  	Print(args ...interface{})
    89  
    90  	// Printf writes a formatted string.
    91  	Printf(format string, args ...interface{})
    92  
    93  	// Detail reports whether error detail is requested.
    94  	// After the first call to Detail, all text written to the Printer
    95  	// is formatted as additional detail, or ignored when
    96  	// detail has not been requested.
    97  	// If Detail returns false, the caller can avoid printing the detail at all.
    98  	Detail() bool
    99  }