-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.go
More file actions
78 lines (65 loc) · 1.61 KB
/
Copy pathfunc.go
File metadata and controls
78 lines (65 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package xerror
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/save95/xerror/xcode"
)
func New(message string) *xError {
return &xError{
code: xcode.NewMessage(message),
error: errors.New(message),
}
}
func WithCode(code int, message string) *xError {
return &xError{
code: xcode.NewWithMessage(code, message),
error: errors.New(message),
}
}
func WithCodef(code int, format string, args ...interface{}) *xError {
return WithCode(code, fmt.Sprintf(format, args...))
}
func WithXCode(code xcode.XCode) *xError {
return &xError{
code: code,
error: errors.New(code.String()),
}
}
func WithXCodeMessage(code xcode.XCode, message string) *xError {
if len(message) > 0 {
code = xcode.WithMessage(code, message)
}
return &xError{
code: code,
error: errors.New(code.String()),
}
}
func WithXCodeMessagef(code xcode.XCode, format string, args ...interface{}) *xError {
return WithXCodeMessage(code, fmt.Sprintf(format, args...))
}
func ParsePayload(err error) string {
if xf, ok := err.(XFields); ok {
fields := xf.GetFields()
if fields != nil && len(fields) > 0 {
xfbs, _ := json.Marshal(fields)
return string(xfbs)
}
}
return ""
}
// FormatStackTrace 格式化错误栈
func FormatStackTrace(err error) string {
return fmt.Sprintf("%+v", err)
}
// FormatMsg 格式化错误消息内容
func FormatMsg(err error) string {
if e, ok := err.(XError); ok {
msg := fmt.Sprintf("[%d] %s", e.ErrorCode(), e.String())
if xf, ok := err.(XFields); ok {
return fmt.Sprintf("[%d] %s (%+v)", e.ErrorCode(), e.String(), xf.GetFields())
}
return msg
}
return err.Error()
}