Skip to content

Commit 1458dce

Browse files
committed
fix: barry quick fix, 2025-06-07 22:01:29
1 parent 86cc1a0 commit 1458dce

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

dixinternal/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"reflect"
66
"strings"
7+
8+
"github.com/pubgo/funk/errors"
79
)
810

911
// DixError 依赖注入相关错误
@@ -110,6 +112,7 @@ func NewConfigurationError(message string) *DixError {
110112

111113
// WrapError 包装现有错误
112114
func WrapError(err error, errType ErrorType, message string) *DixError {
115+
err = errors.WrapCaller(err, 1)
113116
return NewDixError(errType, message).WithCause(err)
114117
}
115118

dixinternal/provider.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,28 @@ func (p *FuncProvider) Invoke(args []reflect.Value) (results []reflect.Value, er
103103
WithDetail("actual", len(args))
104104
}
105105

106+
// 收集入参信息用于错误记录
107+
argTypes := make([]string, len(args))
108+
argValues := make([]interface{}, len(args))
109+
for i, arg := range args {
110+
argTypes[i] = arg.Type().String()
111+
if arg.IsValid() && arg.CanInterface() {
112+
argValues[i] = arg.Interface()
113+
} else {
114+
argValues[i] = "<invalid_or_unexportable>"
115+
}
116+
}
117+
106118
defer func() {
107119
if r := recover(); r != nil {
108120
// 记录调用栈信息
109121
fnStack := stack.CallerWithFunc(p.fn)
110122
logger.Error().
111123
Str("provider", fnStack.String()).
112124
Interface("panic", r).
125+
Strs("input_types", argTypes).
126+
Interface("input_values", argValues).
127+
Str("expected_output_type", p.outputType.String()).
113128
Msg("provider function panicked")
114129

115130
// 将 panic 转换为错误
@@ -122,7 +137,10 @@ func (p *FuncProvider) Invoke(args []reflect.Value) (results []reflect.Value, er
122137
err = WrapError(panicErr, ErrorTypeInvocation, "provider function panicked").
123138
WithDetail("provider_type", p.outputType.String()).
124139
WithDetail("panic_value", r).
125-
WithDetail("provider_location", fnStack.String())
140+
WithDetail("provider_location", fnStack.String()).
141+
WithDetail("input_types", argTypes).
142+
WithDetail("input_values", argValues).
143+
WithDetail("expected_output_type", p.outputType.String())
126144
}
127145
}()
128146

@@ -141,9 +159,23 @@ func (p *FuncProvider) Invoke(args []reflect.Value) (results []reflect.Value, er
141159
errorValue := results[1]
142160
if !errorValue.IsNil() {
143161
// 提取 error 并返回
144-
if err, ok := errorValue.Interface().(error); ok {
145-
return nil, WrapError(err, ErrorTypeProvider, "provider function returned error").
146-
WithDetail("provider_type", p.outputType.String())
162+
if providerErr, ok := errorValue.Interface().(error); ok {
163+
// 记录 provider 返回 error 的详细信息
164+
fnStack := stack.CallerWithFunc(p.fn)
165+
logger.Error().
166+
Str("provider", fnStack.String()).
167+
Err(providerErr).
168+
Strs("input_types", argTypes).
169+
Interface("input_values", argValues).
170+
Str("expected_output_type", p.outputType.String()).
171+
Msg("provider function returned error")
172+
173+
return nil, WrapError(providerErr, ErrorTypeProvider, "provider function returned error").
174+
WithDetail("provider_type", p.outputType.String()).
175+
WithDetail("provider_location", fnStack.String()).
176+
WithDetail("input_types", argTypes).
177+
WithDetail("input_values", argValues).
178+
WithDetail("expected_output_type", p.outputType.String())
147179
}
148180
}
149181
// 如果有 error 返回值但为 nil,只返回第一个值

0 commit comments

Comments
 (0)