github.com/cockroachdb/errors@v1.11.1/errutil/message.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 errutil
    16  
    17  import "github.com/cockroachdb/redact"
    18  
    19  // WithMessage annotates err with a new message.
    20  // If err is nil, WithMessage returns nil.
    21  // The message is considered safe for reporting
    22  // and is included in Sentry reports.
    23  func WithMessage(err error, message string) error {
    24  	if err == nil {
    25  		return nil
    26  	}
    27  	return &withPrefix{
    28  		cause:  err,
    29  		prefix: redact.Sprint(redact.Safe(message)),
    30  	}
    31  }
    32  
    33  // WithMessagef annotates err with the format specifier.
    34  // If err is nil, WithMessagef returns nil.
    35  // The message is formatted as per redact.Sprintf,
    36  // to separate safe and unsafe strings for Sentry reporting.
    37  func WithMessagef(err error, format string, args ...interface{}) error {
    38  	if err == nil {
    39  		return nil
    40  	}
    41  	return &withPrefix{
    42  		cause:  err,
    43  		prefix: redact.Sprintf(format, args...),
    44  	}
    45  }