github.com/matrixorigin/matrixone@v1.2.0/pkg/util/errutil/errors.go (about)

     1  // Copyright 2022 Matrix Origin
     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 implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package errutil
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"sync/atomic"
    21  
    22  	pkgErr "github.com/pkg/errors"
    23  )
    24  
    25  func init() {
    26  	SetErrorReporter(noopReportError)
    27  }
    28  
    29  type Wrapper interface {
    30  	Unwrap() error
    31  }
    32  
    33  type WithIs interface {
    34  	Is(error) bool
    35  }
    36  
    37  func Wrap(err error, message string) error {
    38  	return pkgErr.Wrap(err, message)
    39  }
    40  
    41  func Wrapf(err error, format string, args ...any) error {
    42  	return pkgErr.Wrapf(err, format, args...)
    43  }
    44  
    45  // WalkDeep does a depth-first traversal of all errors.
    46  // The visitor function can return true to end the traversal early, otherwise false.
    47  func WalkDeep(err error, visitor func(err error) bool) bool {
    48  	// Go deep
    49  	unErr := err
    50  	for unErr != nil {
    51  		if done := visitor(unErr); done {
    52  			return true
    53  		}
    54  		unErr = errors.Unwrap(unErr)
    55  	}
    56  
    57  	return false
    58  }
    59  
    60  type noReportKeyType int
    61  
    62  const noReportKey noReportKeyType = iota
    63  
    64  func ContextWithNoReport(parent context.Context, no bool) context.Context {
    65  	return context.WithValue(parent, noReportKey, no)
    66  }
    67  
    68  // NoReportFromContext return false default.
    69  func NoReportFromContext(ctx context.Context) bool {
    70  	if ctx == nil {
    71  		return false
    72  	}
    73  	if noLog, ok := ctx.Value(noReportKey).(bool); ok {
    74  		return noLog
    75  	}
    76  	return false
    77  }
    78  
    79  // ReportError used to handle non-moerr Error
    80  func ReportError(ctx context.Context, err error) {
    81  	if NoReportFromContext(ctx) {
    82  		return
    83  	}
    84  	GetReportErrorFunc()(ctx, err, 1)
    85  }
    86  
    87  type reportErrorFunc func(context.Context, error, int)
    88  
    89  // errorReporter should be trace.ReportError
    90  var errorReporter atomic.Value
    91  
    92  func noopReportError(context.Context, error, int) {}
    93  
    94  func SetErrorReporter(f reportErrorFunc) {
    95  	errorReporter.Store(f)
    96  }
    97  
    98  func GetReportErrorFunc() reportErrorFunc {
    99  	return errorReporter.Load().(reportErrorFunc)
   100  }