github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/testutils/error.go (about)

     1  // Copyright 2015 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 testutils
    12  
    13  import (
    14  	"regexp"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    18  )
    19  
    20  // IsError returns true if the error string matches the supplied regex.
    21  // An empty regex is interpreted to mean that a nil error is expected.
    22  func IsError(err error, re string) bool {
    23  	if err == nil && re == "" {
    24  		return true
    25  	}
    26  	if err == nil || re == "" {
    27  		return false
    28  	}
    29  	errString := pgerror.FullError(err)
    30  	matched, merr := regexp.MatchString(re, errString)
    31  	if merr != nil {
    32  		return false
    33  	}
    34  	return matched
    35  }
    36  
    37  // IsPError returns true if pErr's message matches the supplied regex.
    38  // An empty regex is interpreted to mean that a nil error is expected.
    39  func IsPError(pErr *roachpb.Error, re string) bool {
    40  	if pErr == nil && re == "" {
    41  		return true
    42  	}
    43  	if pErr == nil || re == "" {
    44  		return false
    45  	}
    46  	matched, merr := regexp.MatchString(re, pErr.Message)
    47  	if merr != nil {
    48  		return false
    49  	}
    50  	return matched
    51  }