-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
95 lines (78 loc) · 1.79 KB
/
error.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package exterr
import (
"encoding/json"
"fmt"
"runtime"
)
// Error is an extended error main struct
type Error struct {
Err string `json:"message,omitempty"`
ErrCode int `json:"code,omitempty"`
Comp string `json:"component,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Cause error
}
// NewErrorWithMessage creates Error type with string in parameter
func NewErrorWithMessage(msg string) *Error {
err := Error{Err: msg}
err.setFrame()
return &err
}
// NewErrorWithErr creates Error type with error in parameter
func NewErrorWithErr(err error) *Error {
if err == nil {
panic("err parameter is nil")
}
newErr := Error{Err: err.Error()}
newErr.setFrame()
return &newErr
}
func (e *Error) setFrameWithSkip(skip int) {
_, file, line, ok := runtime.Caller(skip)
if !ok {
return
}
e.File = file
e.Line = line
}
func (e *Error) setFrame() {
e.setFrameWithSkip(3)
}
// WithComponent sets component name of Error
func (e *Error) WithComponent(component string) *Error {
e.Comp = component
return e
}
// WithCode sets error code of Error
func (e *Error) WithCode(code int) *Error {
e.ErrCode = code
return e
}
// Error prints component and error code
func (e *Error) Error() string {
var errorString string
if len(e.Comp) > 0 {
errorString = e.Comp + "-"
}
if e.ErrCode != 0 {
errorString = errorString + fmt.Sprintf("%04d", e.ErrCode) + " "
}
return errorString + e.Err
}
// Code is getter to error code
func (e *Error) Code() int {
return e.ErrCode
}
// Message is getter to message
func (e *Error) Message() string {
return e.Err
}
// Component is getter to component
func (e *Error) Component() string {
return e.Comp
}
// JSON returns json representation
func (e *Error) JSON() ([]byte, error) {
return json.Marshal(e)
}