github.com/matrixorigin/matrixone@v0.7.0/pkg/util/errutil/context.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  	"fmt"
    20  
    21  	"github.com/cockroachdb/errors/errbase"
    22  	"github.com/matrixorigin/matrixone/pkg/util/stack"
    23  )
    24  
    25  // WithContext annotates err with a stack info and a context, which should contain span info.
    26  // At the mean time, it will call ReportError to store error info
    27  // If err is nil, WithContext returns nil.
    28  func WithContext(ctx context.Context, err error) error {
    29  	return WithContextWithDepth(ctx, err, 1)
    30  }
    31  
    32  func WithContextWithDepth(ctx context.Context, err error, depth int) error {
    33  	if err == nil {
    34  		return nil
    35  	}
    36  	if _, ok := err.(StackTracer); !ok {
    37  		err = &withStack{cause: err, Stack: stack.Callers(depth + 1)}
    38  	}
    39  	err = &withContext{cause: err, ctx: ctx}
    40  	GetReportErrorFunc()(ctx, err, depth+1)
    41  	return err
    42  }
    43  
    44  // ContextTracer retrieves the context.Context
    45  type ContextTracer interface {
    46  	Context() context.Context
    47  }
    48  
    49  func GetContextTracer(inErr error) ContextTracer {
    50  	var stacked ContextTracer
    51  	WalkDeep(inErr, func(err error) bool {
    52  		if contextTracer, ok := err.(ContextTracer); ok {
    53  			stacked = contextTracer
    54  			return true
    55  		}
    56  		return false
    57  	})
    58  	return stacked
    59  }
    60  
    61  func HasContext(err error) bool {
    62  	return GetContextTracer(err) != nil
    63  }
    64  
    65  var _ error = (*withContext)(nil)
    66  var _ Wrapper = (*withContext)(nil)
    67  var _ ContextTracer = (*withContext)(nil)
    68  var _ fmt.Formatter = (*withContext)(nil)
    69  
    70  type withContext struct {
    71  	cause error
    72  
    73  	ctx context.Context
    74  }
    75  
    76  func (w *withContext) Error() string                 { return w.cause.Error() }
    77  func (w *withContext) Cause() error                  { return w.cause }
    78  func (w *withContext) Unwrap() error                 { return w.cause }
    79  func (w *withContext) Context() context.Context      { return w.ctx }
    80  func (w *withContext) Format(s fmt.State, verb rune) { errbase.FormatError(w, s, verb) }