add switch of brief stack for package gerror (#2153)

This commit is contained in:
John Guo
2022-09-27 10:11:33 +08:00
committed by GitHub
parent 141ca62c6d
commit 66aa0c7050
9 changed files with 42 additions and 18 deletions

View File

@ -13,6 +13,7 @@ package gerror
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/internal/command"
)
// IIs is the interface for Is feature.
@ -56,3 +57,20 @@ type IUnwrap interface {
Error() string
Unwrap() error
}
const (
// commandEnvKeyForBrief is the command environment name for switch key for brief error stack.
commandEnvKeyForBrief = "gf.gerror.brief"
)
var (
// isUsingBriefStack is the switch key for brief error stack.
isUsingBriefStack bool
)
func init() {
value := command.GetOptWithEnv(commandEnvKeyForBrief)
if value == "1" || value == "true" {
isUsingBriefStack = true
}
}

View File

@ -15,7 +15,7 @@ type stack []uintptr
const (
// maxStackDepth marks the max stack depth for error back traces.
maxStackDepth = 32
maxStackDepth = 64
)
// Cause returns the root cause error of `err`.

View File

@ -12,6 +12,8 @@ import (
"io"
"runtime"
"strings"
"github.com/gogf/gf/v2/internal/consts"
)
// Format formats the frame according to the fmt.Formatter interface.
@ -52,9 +54,16 @@ func formatSubStack(st stack, buffer *bytes.Buffer) {
for _, p := range st {
if fn := runtime.FuncForPC(p - 1); fn != nil {
file, line := fn.FileLine(p - 1)
// Custom filtering.
if strings.Contains(file, stackFilterKeyLocal) {
continue
if isUsingBriefStack {
// filter whole GoFrame packages stack paths.
if strings.Contains(file, consts.StackFilterKeyForGoFrame) {
continue
}
} else {
// package path stack filtering.
if strings.Contains(file, stackFilterKeyLocal) {
continue
}
}
// Avoid stack string like "`autogenerated`"
if strings.Contains(file, "<") {