Skip to content

Commit 7e0f979

Browse files
committed
fix: barry quick fix, 2025-09-06 01:52:47
1 parent a7579d2 commit 7e0f979

File tree

8 files changed

+82
-31
lines changed

8 files changed

+82
-31
lines changed

errors/aaa.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"google.golang.org/grpc/status"
8-
"google.golang.org/protobuf/proto"
7+
"github.com/pubgo/funk/errors/errinter"
98
)
109

1110
var (
@@ -64,9 +63,7 @@ func (t Tag) String() string {
6463
return fmt.Sprintf("%s: %v", t.K, t.V)
6564
}
6665

67-
type ErrUnwrap interface {
68-
Unwrap() error
69-
}
66+
type ErrUnwrap errinter.ErrUnwrap
7067

7168
type ErrIs interface {
7269
Is(error) bool
@@ -76,18 +73,6 @@ type ErrAs interface {
7673
As(any) bool
7774
}
7875

79-
type Error interface {
80-
ID() string
81-
Kind() string
82-
Error() string
83-
String() string
84-
MarshalJSON() ([]byte, error)
85-
}
86-
87-
type ErrorProto interface {
88-
Proto() proto.Message
89-
}
90-
91-
type GRPCStatus interface {
92-
GRPCStatus() *status.Status
93-
}
76+
type Error = errinter.Error
77+
type ErrorProto = errinter.ErrorProto
78+
type GRPCStatus = errinter.GRPCStatus

errors/errinter/aaa.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package errinter
2+
3+
import (
4+
"google.golang.org/grpc/status"
5+
"google.golang.org/protobuf/proto"
6+
)
7+
8+
type Error interface {
9+
ID() string
10+
Kind() string
11+
Error() string
12+
String() string
13+
MarshalJSON() ([]byte, error)
14+
}
15+
16+
type ErrUnwrap interface {
17+
Unwrap() error
18+
}
19+
20+
type ErrorProto interface {
21+
Proto() proto.Message
22+
}
23+
24+
type GRPCStatus interface {
25+
GRPCStatus() *status.Status
26+
}

errors/errinter/utils.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package errinter
22

33
import (
44
"errors"
5+
"fmt"
56
"strings"
67
"sync"
78

89
"github.com/k0kubun/pp/v3"
10+
"google.golang.org/protobuf/proto"
11+
912
"github.com/pubgo/funk"
13+
"github.com/pubgo/funk/proto/errorpb"
1014
)
1115

1216
func ParseError(val interface{}) error {
@@ -40,3 +44,42 @@ var Simple = sync.OnceValue(func() *pp.PrettyPrinter {
4044
func SimplePrint(v interface{}) string {
4145
return strings.ReplaceAll(Simple().Sprint(v), "\n", "")
4246
}
47+
48+
func GetErrorId(err error) string {
49+
if err == nil {
50+
return ""
51+
}
52+
53+
for err != nil {
54+
if v, ok := err.(Error); ok {
55+
return v.ID()
56+
}
57+
58+
err = Unwrap(err)
59+
}
60+
61+
return ""
62+
}
63+
64+
func Unwrap(err error) error {
65+
u, ok := err.(ErrUnwrap)
66+
if !ok {
67+
return nil
68+
}
69+
return u.Unwrap()
70+
}
71+
72+
func ParseErrToPb(err error) proto.Message {
73+
switch err1 := err.(type) {
74+
case nil:
75+
return nil
76+
case ErrorProto:
77+
return err1.Proto()
78+
case GRPCStatus:
79+
return err1.GRPCStatus().Proto()
80+
case proto.Message:
81+
return err1
82+
default:
83+
return &errorpb.ErrMsg{Msg: err.Error(), Detail: fmt.Sprintf("%v", err)}
84+
}
85+
}

log/global.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
"github.com/pubgo/funk/assert"
11-
"github.com/pubgo/funk/errors"
11+
"github.com/pubgo/funk/errors/errinter"
1212
"github.com/pubgo/funk/generic"
1313

1414
"github.com/rs/zerolog"
@@ -38,7 +38,7 @@ var (
3838
}
3939

4040
errDetail := errDetail(err)
41-
id := errors.GetErrorId(err)
41+
id := errinter.GetErrorId(err)
4242
if id != "" {
4343
return fmt.Sprintf("%s, error_id:%s error_detail:%s", err.Error(), id, errDetail)
4444
}
@@ -56,7 +56,7 @@ var (
5656
Output(zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
5757
w.Out = os.Stderr
5858
w.TimeFormat = time.RFC3339
59-
})).Hook(new(hookImpl), logGlobalHook),
59+
})).Hook(logGlobalHook),
6060
)
6161

6262
_ = generic.Init(func() {

log/impl.log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/rs/zerolog"
99

10-
"github.com/pubgo/funk/errors"
10+
"github.com/pubgo/funk/errors/errinter"
1111
"github.com/pubgo/funk/log/logfields"
1212
)
1313

@@ -146,7 +146,7 @@ func (l *loggerImpl) Err(err error, ctxL ...context.Context) *zerolog.Event {
146146
return
147147
}
148148

149-
if id := errors.GetErrorId(err); id != "" {
149+
if id := errinter.GetErrorId(err); id != "" {
150150
e.Str("error_id", id)
151151
}
152152

log/hooks.go renamed to log/loghooks/hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package log
1+
package loghooks
22

33
import (
44
"sync/atomic"

log/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package log
33
import (
44
"context"
55

6-
"github.com/pubgo/funk/errors"
6+
"github.com/pubgo/funk/errors/errinter"
77
"github.com/samber/lo"
88
"google.golang.org/protobuf/encoding/prototext"
99
)
@@ -13,7 +13,7 @@ func errDetail(err error) string {
1313
return ""
1414
}
1515

16-
return prototext.Format(errors.ParseErrToPb(err))
16+
return prototext.Format(errinter.ParseErrToPb(err))
1717
}
1818

1919
func RecordErr(logs ...Logger) func(ctx context.Context, err error) error {

log/z_log_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ func TestWithName(t *testing.T) {
2121
Func(func(e *zerolog.Event) {
2222
var buf = gjson.ParseBytes(log.GetEventBuf(e))
2323
assert.Equal(t, buf.Get("logger").String(), "log1")
24-
assert.Equal(t, buf.Get("module").String(), "github.com/pubgo/funk/log_test")
2524
}).Msg("hello")
2625

2726
log.GetLogger("log1").
@@ -30,14 +29,12 @@ func TestWithName(t *testing.T) {
3029
Func(func(e *zerolog.Event) {
3130
var buf = gjson.ParseBytes(log.GetEventBuf(e))
3231
assert.Equal(t, buf.Get("logger").String(), "log1.log2")
33-
assert.Equal(t, buf.Get("module").String(), "github.com/pubgo/funk/log_test")
3432
}).Msg("hello")
3533

3634
log.Debug().
3735
Func(func(e *zerolog.Event) {
3836
var buf = gjson.ParseBytes(log.GetEventBuf(e))
3937
assert.Equal(t, buf.Get("logger").String(), "")
40-
assert.Equal(t, buf.Get("module").String(), "")
4138
}).Msg("hello")
4239
}
4340

0 commit comments

Comments
 (0)