-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorg.go
56 lines (44 loc) · 1.28 KB
/
errorg.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
package errorg
import (
"encoding/json"
)
// errorsMap holds errors that implemented group's errors
var errorsMap = map[string]map[string]interface{}{}
// errorg represents errorg struct
// Groyp represents the group ex: http,go etc..
// Code represents the specific code for given groyp ex: "http","404" => "Not found"
type errorg struct {
Group string
Code string
}
// Error return error according to given information's
func (e errorg) Error() string {
return e.prepare()
}
// prepare returns result string that is changed
func (e errorg) prepare() string {
var errInfo errorInfo
currentLog := defaultConfigs[log]
errStruct := errorsMap[e.Group][e.Code]
marshal, _ := json.Marshal(errStruct)
if currentLog == logTypeMap[jsonType] {
return string(marshal)
}
err := json.Unmarshal(marshal, &errInfo)
if err != nil {
return "an error occurred when unmarshalling error info: " + err.Error()
}
return errInfo.Msg
}
// Init initializes the errorg
// This method accepts options hereby it can be set options
func Init(options ...Options) {
directoryInitializer()
configInitializer()
optionsInitializer(options)
groupsInitializer()
}
// New return builtin error according to given values
func New(group string, code string) error {
return &errorg{Group: group, Code: code}
}