@@ -5,10 +5,10 @@ import (
55 "errors"
66 "fmt"
77 "os"
8- "strings"
98 "sync"
109
1110 "github.com/k0kubun/pp/v3"
11+ "github.com/rs/xid"
1212 "github.com/rs/zerolog/log"
1313 "github.com/samber/lo"
1414 "google.golang.org/protobuf/encoding/prototext"
@@ -18,7 +18,6 @@ import (
1818
1919 "github.com/pubgo/funk"
2020 "github.com/pubgo/funk/log/logutil"
21- "github.com/pubgo/funk/pretty"
2221 "github.com/pubgo/funk/proto/errorpb"
2322)
2423
@@ -36,38 +35,19 @@ func ParseError(val interface{}) error {
3635 return errors .New (v )
3736 case []byte :
3837 return errors .New (string (v ))
38+ case proto.Message :
39+ return errors .New (prototext .Format (v ))
3940 default :
40- return errors .New (SimplePrint (v ))
41+ return errors .New (errPretty (). Sprint (v ))
4142 }
4243}
4344
44- var Simple = sync .OnceValue (func () * pp.PrettyPrinter {
45- printer := pp .New ()
46- printer .SetColoringEnabled (false )
47- printer .SetExportedOnly (false )
48- printer .SetOmitEmpty (true )
49- printer .SetMaxDepth (3 )
50- return printer
51- })
52-
53- func SimplePrint (v interface {}) string {
54- return strings .ReplaceAll (Simple ().Sprint (v ), "\n " , "" )
55- }
56-
5745func GetErrorId (err error ) string {
58- if err == nil {
59- return ""
60- }
61-
62- for err != nil {
63- if v , ok := err .(Error ); ok {
64- return v .ID ()
65- }
66-
67- err = Unwrap (err )
46+ if v , ok := lo.ErrorsAs [Error ](err ); ok && v != nil {
47+ return v .ID ()
6848 }
6949
70- return ""
50+ return xid . New (). String ()
7151}
7252
7353func Unwrap (err error ) error {
@@ -103,8 +83,7 @@ func Debug(err error) {
10383 return
10484 }
10585
106- pretty .SetDefaultMaxDepth (20 )
107- pretty .Println (err )
86+ Console ().Println (err )
10887}
10988
11089func MustTagsToAny (tags ... * errorpb.Tag ) []* anypb.Any {
@@ -171,3 +150,21 @@ func MustProtoToAny(p proto.Message) *anypb.Any {
171150
172151 return pb
173152}
153+
154+ var errPretty = sync .OnceValue (func () * pp.PrettyPrinter {
155+ printer := pp .New ()
156+ printer .SetColoringEnabled (false )
157+ printer .SetExportedOnly (false )
158+ printer .SetOmitEmpty (true )
159+ printer .SetMaxDepth (3 )
160+ return printer
161+ })
162+
163+ var Console = sync .OnceValue (func () * pp.PrettyPrinter {
164+ printer := pp .New ()
165+ printer .SetColoringEnabled (true )
166+ printer .SetExportedOnly (false )
167+ printer .SetOmitEmpty (true )
168+ printer .SetMaxDepth (5 )
169+ return printer
170+ })
0 commit comments