Skip to content

Commit

Permalink
improve error stack configuration for package gerror, add framework e…
Browse files Browse the repository at this point in the history
…rror stack filter for package glog (#2918)
  • Loading branch information
gqcn committed Aug 31, 2023
1 parent 7d4c59a commit 3f69e0d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
16 changes: 0 additions & 16 deletions errors/gerror/gerror.go
Expand Up @@ -13,7 +13,6 @@ package gerror

import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/internal/command"
)

// IIs is the interface for Is feature.
Expand Down Expand Up @@ -59,21 +58,6 @@ type IUnwrap interface {
}

const (
// commandEnvKeyForBrief is the command environment name for switch key for brief error stack.
commandEnvKeyForBrief = "gf.gerror.brief"

// commaSeparatorSpace is the comma separator with space.
commaSeparatorSpace = ", "
)

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
}
}
14 changes: 8 additions & 6 deletions errors/gerror/gerror_error_stack.go
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/errors"
)

// stackInfo manages stack info of certain error.
Expand All @@ -35,9 +36,10 @@ func (err *Error) Stack() string {
return ""
}
var (
loop = err
index = 1
infos []*stackInfo
loop = err
index = 1
infos []*stackInfo
isStackModeBrief = errors.IsStackModeBrief()
)
for loop != nil {
info := &stackInfo{
Expand All @@ -46,7 +48,7 @@ func (err *Error) Stack() string {
}
index++
infos = append(infos, info)
loopLinesOfStackInfo(loop.stack, info)
loopLinesOfStackInfo(loop.stack, info, isStackModeBrief)
if loop.error != nil {
if e, ok := loop.error.(*Error); ok {
loop = e
Expand Down Expand Up @@ -131,14 +133,14 @@ func formatStackLines(buffer *bytes.Buffer, lines *list.List) string {
}

// loopLinesOfStackInfo iterates the stack info lines and produces the stack line info.
func loopLinesOfStackInfo(st stack, info *stackInfo) {
func loopLinesOfStackInfo(st stack, info *stackInfo, isStackModeBrief bool) {
if st == nil {
return
}
for _, p := range st {
if fn := runtime.FuncForPC(p - 1); fn != nil {
file, line := fn.FileLine(p - 1)
if isUsingBriefStack {
if isStackModeBrief {
// filter whole GoFrame packages stack paths.
if strings.Contains(file, consts.StackFilterKeyForGoFrame) {
continue
Expand Down
61 changes: 61 additions & 0 deletions internal/errors/errors.go
@@ -0,0 +1,61 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package errors provides functionalities to manipulate errors for internal usage purpose.
package errors

import (
"github.com/gogf/gf/v2/internal/command"
)

// StackMode is the mode that printing stack information in StackModeBrief or StackModeDetail mode.
type StackMode string

const (
// commandEnvKeyForBrief is the command environment name for switch key for brief error stack.
// Deprecated: use commandEnvKeyForStackMode instead.
commandEnvKeyForBrief = "gf.gerror.brief"

// commandEnvKeyForStackMode is the command environment name for switch key for brief error stack.
commandEnvKeyForStackMode = "gf.gerror.stack.mode"
)

const (
// StackModeBrief specifies all error stacks printing no framework error stacks.
StackModeBrief StackMode = "brief"

// StackModeDetail specifies all error stacks printing detailed error stacks including framework stacks.
StackModeDetail StackMode = "detail"
)

var (
// stackModeConfigured is the configured error stack mode variable.
// It is brief stack mode in default.
stackModeConfigured = StackModeBrief
)

func init() {
// Deprecated.
briefSetting := command.GetOptWithEnv(commandEnvKeyForBrief)
if briefSetting == "1" || briefSetting == "true" {
stackModeConfigured = StackModeBrief
}

// The error stack mode is configured using command line arguments or environments.
stackModeSetting := command.GetOptWithEnv(commandEnvKeyForStackMode)
if stackModeSetting != "" {
stackModeSettingMode := StackMode(stackModeSetting)
switch stackModeSettingMode {
case StackModeBrief, StackModeDetail:
stackModeConfigured = stackModeSettingMode
}
}
}

// IsStackModeBrief returns whether current error stack mode is in brief mode.
func IsStackModeBrief() bool {
return stackModeConfigured == StackModeBrief
}
20 changes: 20 additions & 0 deletions internal/errors/errors_test.go
@@ -0,0 +1,20 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package errors_test

import (
"testing"

"github.com/gogf/gf/v2/internal/errors"
"github.com/gogf/gf/v2/test/gtest"
)

func Test_IsStackModeBrief(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(errors.IsStackModeBrief(), true)
})
}
5 changes: 5 additions & 0 deletions os/glog/glog_logger.go
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/errors"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
Expand Down Expand Up @@ -411,5 +412,9 @@ func (l *Logger) GetStack(skip ...int) string {
if l.config.StFilter != "" {
filters = append(filters, l.config.StFilter)
}
// Whether filter framework error stacks.
if errors.IsStackModeBrief() {
filters = append(filters, consts.StackFilterKeyForGoFrame)
}
return gdebug.StackWithFilters(filters, stackSkip)
}

0 comments on commit 3f69e0d

Please sign in to comment.