github.com/matrixorigin/matrixone@v1.2.0/pkg/util/errutil/stack.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 "fmt" 19 20 "github.com/matrixorigin/matrixone/pkg/util/stack" 21 22 "github.com/cockroachdb/errors/errbase" 23 ) 24 25 // StackTracer retrieves the StackTrace 26 // Generally you would want to use the GetStackTracer function to do that. 27 type StackTracer interface { 28 StackTrace() stack.StackTrace 29 } 30 31 func GetStackTracer(inErr error) StackTracer { 32 var stacked StackTracer 33 WalkDeep(inErr, func(err error) bool { 34 if stackTracer, ok := err.(StackTracer); ok { 35 stacked = stackTracer 36 return true 37 } 38 return false 39 }) 40 return stacked 41 } 42 43 func HasStack(err error) bool { 44 return GetStackTracer(err) != nil 45 } 46 47 type withStack struct { 48 cause error 49 50 *stack.Stack 51 } 52 53 var _ error = (*withStack)(nil) 54 var _ Wrapper = (*withStack)(nil) 55 var _ fmt.Formatter = (*withStack)(nil) 56 var _ StackTracer = (*withStack)(nil) 57 58 func (w *withStack) Error() string { return w.cause.Error() } 59 func (w *withStack) Cause() error { return w.cause } 60 func (w *withStack) Unwrap() error { return w.cause } 61 62 // Format implements the fmt.Formatter interface. 63 func (w *withStack) Format(s fmt.State, verb rune) { errbase.FormatError(w, s, verb) } 64 65 func (w *withStack) HasStack() bool { 66 return true 67 }