github.com/cockroachdb/errors@v1.11.1/errbase/safe_details.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  package errbase
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/cockroachdb/errors/errorspb"
    21  	pkgErr "github.com/pkg/errors"
    22  )
    23  
    24  // SafeDetailer is an interface that can be implemented by errors that
    25  // can provide PII-free additional strings suitable for reporting or
    26  // telemetry.
    27  type SafeDetailer interface {
    28  	SafeDetails() []string
    29  }
    30  
    31  // GetAllSafeDetails collects the safe details from the given error object
    32  // and all its causes.
    33  // The details are collected from outermost to innermost level of cause.
    34  func GetAllSafeDetails(err error) []SafeDetailPayload {
    35  	var details []SafeDetailPayload
    36  	for ; err != nil; err = UnwrapOnce(err) {
    37  		details = append(details, GetSafeDetails(err))
    38  	}
    39  	return details
    40  }
    41  
    42  // GetSafeDetails collects the safe details from the given error
    43  // object. If it is a wrapper, only the details from the wrapper are
    44  // returned.
    45  func GetSafeDetails(err error) (payload SafeDetailPayload) {
    46  	origTypeName, famName, ext := getTypeDetails(err, false /*onlyFamily*/)
    47  	payload.OriginalTypeName = origTypeName
    48  	payload.ErrorTypeMark = errorspb.ErrorTypeMark{
    49  		FamilyName: famName,
    50  		Extension:  ext,
    51  	}
    52  	payload.SafeDetails = getDetails(err)
    53  	return
    54  }
    55  
    56  func getDetails(err error) []string {
    57  	if sd, ok := err.(SafeDetailer); ok {
    58  		return sd.SafeDetails()
    59  	}
    60  	// For convenience, we also know how to extract stack traces
    61  	// in the style of github.com/pkg/errors.
    62  	if st, ok := err.(interface{ StackTrace() pkgErr.StackTrace }); ok {
    63  		return []string{fmt.Sprintf("%+v", st.StackTrace())}
    64  	}
    65  	return nil
    66  }
    67  
    68  // SafeDetailPayload captures the safe strings for one
    69  // level of wrapping.
    70  type SafeDetailPayload struct {
    71  	// OriginalTypeName is the concrete type of the error that the details
    72  	// are coming from.
    73  	OriginalTypeName string
    74  	// ErrorTypeMark is the mark of the error that the details are
    75  	// coming from. This may contain a different type name than
    76  	// OriginalTypeName in case an error type was migrated.
    77  	ErrorTypeMark errorspb.ErrorTypeMark
    78  	// SafeDetails are the PII-free strings.
    79  	SafeDetails []string
    80  }
    81  
    82  // Fill can be used to concatenate multiple SafeDetailPayloads.
    83  func (s *SafeDetailPayload) Fill(slice []string) []string {
    84  	if len(s.SafeDetails) == 0 {
    85  		return slice
    86  	}
    87  	slice = append(slice, fmt.Sprintf("details for %s::%s:",
    88  		s.ErrorTypeMark.FamilyName, s.ErrorTypeMark.Extension))
    89  	for _, sd := range s.SafeDetails {
    90  		slice = append(slice, "  "+sd)
    91  	}
    92  	return slice
    93  }