github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/pgerror/wrap.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package pgerror
    12  
    13  import "github.com/cockroachdb/errors"
    14  
    15  // Wrapf wraps an error and adds a pg error code. See
    16  // the doc on WrapWithDepthf for details.
    17  func Wrapf(err error, code, format string, args ...interface{}) error {
    18  	return WrapWithDepthf(1, err, code, format, args...)
    19  }
    20  
    21  // WrapWithDepthf wraps an error. It also annotates the provided
    22  // pg code as new candidate code, to be used if the underlying
    23  // error does not have one already.
    24  func WrapWithDepthf(depth int, err error, code, format string, args ...interface{}) error {
    25  	err = errors.WrapWithDepthf(1+depth, err, format, args...)
    26  	err = WithCandidateCode(err, code)
    27  	return err
    28  }
    29  
    30  // Wrap wraps an error and adds a pg error code. Only the code
    31  // is added if the message is empty.
    32  func Wrap(err error, code, msg string) error {
    33  	if msg == "" {
    34  		return WithCandidateCode(err, code)
    35  	}
    36  	return WrapWithDepthf(1, err, code, "%s", msg)
    37  }