Skip to content

Commit 171de64

Browse files
committed
fix: barry quick fix, 2025-09-06 13:36:08
1 parent 2aeede8 commit 171de64

File tree

9 files changed

+198
-241
lines changed

9 files changed

+198
-241
lines changed

errors/aaa.go

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,15 @@
11
package errors
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
74
"github.com/pubgo/funk/errors/errinter"
85
)
96

10-
var (
11-
_ json.Marshaler = (Tags)(nil)
12-
_ fmt.Formatter = (Tags)(nil)
13-
)
14-
15-
type Maps map[string]any
16-
17-
func (t Maps) Tags() Tags {
18-
tags := make(Tags, 0, len(t))
19-
for k, v := range t {
20-
tags = append(tags, Tag{K: k, V: v})
21-
}
22-
return tags
23-
}
24-
25-
type Tags []Tag
26-
27-
func (t Tags) Format(f fmt.State, verb rune) {
28-
tags := make(map[string]any, len(t))
29-
for i := range t {
30-
tags[t[i].K] = t[i].V
31-
}
32-
33-
data, err := json.Marshal(tags)
34-
if err != nil {
35-
fmt.Fprintf(f, "%v", err)
36-
} else {
37-
fmt.Fprintln(f, string(data))
38-
}
39-
}
40-
41-
func (t Tags) ToMap() map[string]string {
42-
var data = make(map[string]string, len(t))
43-
for _, tag := range t {
44-
data[tag.K] = fmt.Sprintf("%v", tag.V)
45-
}
46-
return data
47-
}
48-
49-
func (t Tags) MarshalJSON() ([]byte, error) {
50-
tags := make(map[string]any, len(t))
51-
for i := range t {
52-
tags[t[i].K] = t[i].V
53-
}
54-
return json.Marshal(tags)
55-
}
56-
57-
type Tag struct {
58-
K string
59-
V any
60-
}
61-
62-
func (t Tag) String() string {
63-
return fmt.Sprintf("%s: %v", t.K, t.V)
64-
}
65-
66-
type ErrUnwrap errinter.ErrUnwrap
67-
68-
type ErrIs interface {
69-
Is(error) bool
70-
}
71-
72-
type ErrAs interface {
73-
As(any) bool
74-
}
75-
7+
type Maps = errinter.Maps
8+
type Tags = errinter.Tags
9+
type Tag = errinter.Tag
10+
type ErrIs = errinter.ErrIs
11+
type ErrAs = errinter.ErrAs
12+
type ErrUnwrap = errinter.ErrUnwrap
7613
type Error = errinter.Error
7714
type ErrorProto = errinter.ErrorProto
7815
type GRPCStatus = errinter.GRPCStatus

errors/errinter/aaa.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package errinter
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
47
"google.golang.org/grpc/status"
58
"google.golang.org/protobuf/proto"
69
)
@@ -24,3 +27,67 @@ type ErrorProto interface {
2427
type GRPCStatus interface {
2528
GRPCStatus() *status.Status
2629
}
30+
31+
var (
32+
_ json.Marshaler = (Tags)(nil)
33+
_ fmt.Formatter = (Tags)(nil)
34+
)
35+
36+
type Maps map[string]any
37+
38+
func (t Maps) Tags() Tags {
39+
tags := make(Tags, 0, len(t))
40+
for k, v := range t {
41+
tags = append(tags, Tag{K: k, V: v})
42+
}
43+
return tags
44+
}
45+
46+
type Tags []Tag
47+
48+
func (t Tags) Format(f fmt.State, verb rune) {
49+
tags := make(map[string]any, len(t))
50+
for i := range t {
51+
tags[t[i].K] = t[i].V
52+
}
53+
54+
data, err := json.Marshal(tags)
55+
if err != nil {
56+
fmt.Fprintf(f, "%v", err)
57+
} else {
58+
fmt.Fprintln(f, string(data))
59+
}
60+
}
61+
62+
func (t Tags) ToMap() map[string]string {
63+
var data = make(map[string]string, len(t))
64+
for _, tag := range t {
65+
data[tag.K] = fmt.Sprintf("%v", tag.V)
66+
}
67+
return data
68+
}
69+
70+
func (t Tags) MarshalJSON() ([]byte, error) {
71+
tags := make(map[string]any, len(t))
72+
for i := range t {
73+
tags[t[i].K] = t[i].V
74+
}
75+
return json.Marshal(tags)
76+
}
77+
78+
type Tag struct {
79+
K string
80+
V any
81+
}
82+
83+
func (t Tag) String() string {
84+
return fmt.Sprintf("%s: %v", t.K, t.V)
85+
}
86+
87+
type ErrIs interface {
88+
Is(error) bool
89+
}
90+
91+
type ErrAs interface {
92+
As(any) bool
93+
}

errors/errinter/utils.go

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package errinter
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
7+
"os"
68
"strings"
79
"sync"
810

911
"github.com/k0kubun/pp/v3"
12+
"github.com/rs/zerolog/log"
13+
"github.com/samber/lo"
14+
"google.golang.org/protobuf/encoding/prototext"
1015
"google.golang.org/protobuf/proto"
11-
16+
"google.golang.org/protobuf/types/known/anypb"
17+
"google.golang.org/protobuf/types/known/structpb"
18+
1219
"github.com/pubgo/funk"
20+
"github.com/pubgo/funk/log/logutil"
21+
"github.com/pubgo/funk/pretty"
1322
"github.com/pubgo/funk/proto/errorpb"
1423
)
1524

@@ -83,3 +92,82 @@ func ParseErrToPb(err error) proto.Message {
8392
return &errorpb.ErrMsg{Msg: err.Error(), Detail: fmt.Sprintf("%v", err)}
8493
}
8594
}
95+
96+
func Debug(err error) {
97+
if err == nil {
98+
return
99+
}
100+
101+
if _err, ok := err.(fmt.Stringer); ok {
102+
_, _ = fmt.Fprintln(os.Stderr, _err.String())
103+
return
104+
}
105+
106+
pretty.SetDefaultMaxDepth(20)
107+
pretty.Println(err)
108+
}
109+
110+
func MustTagsToAny(tags ...*errorpb.Tag) []*anypb.Any {
111+
if len(tags) == 0 {
112+
return nil
113+
}
114+
115+
return lo.Map(tags, func(item *errorpb.Tag, index int) *anypb.Any {
116+
return MustProtoToAny(item)
117+
})
118+
}
119+
120+
func MustStructToAny(p map[string]any) *anypb.Any {
121+
if p == nil {
122+
return nil
123+
}
124+
125+
pb, err := structpb.NewStruct(p)
126+
if err != nil {
127+
log.Err(err).
128+
Any("params", p).
129+
Func(logutil.WithNotice()).
130+
Stack().
131+
Msgf("failed to encode map-any to struct protobuf")
132+
return anyToProtobuf(p)
133+
} else {
134+
return MustProtoToAny(pb)
135+
}
136+
}
137+
138+
func anyToProtobuf(v any) *anypb.Any {
139+
data, err := json.Marshal(v)
140+
if err != nil {
141+
return &anypb.Any{
142+
TypeUrl: "type.googleapis.com/google.protobuf.StringValue",
143+
Value: []byte(fmt.Sprintf("err:%s detail:%#v", err.Error(), v)),
144+
}
145+
}
146+
return &anypb.Any{
147+
TypeUrl: "type.googleapis.com/google.protobuf.Struct",
148+
Value: data,
149+
}
150+
}
151+
152+
func MustProtoToAny(p proto.Message) *anypb.Any {
153+
switch p := p.(type) {
154+
case nil:
155+
return nil
156+
case *anypb.Any:
157+
return p
158+
}
159+
160+
pb, err := anypb.New(p)
161+
if err != nil {
162+
params := prototext.Format(p)
163+
log.Err(err).
164+
Str("protobuf", params).
165+
Func(logutil.WithNotice()).
166+
Stack().
167+
Msgf("failed to encode protobuf message to any protobuf")
168+
pb, _ = anypb.New(structpb.NewStringValue(params))
169+
return pb
170+
}
171+
172+
return pb
173+
}

0 commit comments

Comments
 (0)