-
Notifications
You must be signed in to change notification settings - Fork 9
/
err.go
103 lines (86 loc) · 1.6 KB
/
err.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
96
97
98
99
100
101
102
103
package gorose
import (
"errors"
"fmt"
)
// Error ...
type Error uint
// Lang ...
type Lang uint
const (
// CHINESE ...
CHINESE Lang = iota
// ENGLISH ...
ENGLISH
// CHINESE_TRADITIONAL ...
CHINESE_TRADITIONAL
)
const (
// ERR_PARAMS_COUNTS ...
ERR_PARAMS_COUNTS Error = iota
// ERR_PARAMS_MISSING ...
ERR_PARAMS_MISSING
// ERR_PARAMS_FORMAT ...
ERR_PARAMS_FORMAT
)
// Default ...
func (e *Err) Default() map[Error]string {
return map[Error]string{
ERR_PARAMS_COUNTS: "参数数量有误",
ERR_PARAMS_MISSING: "参数缺失",
ERR_PARAMS_FORMAT: "参数格式错误",
}
}
var langString = map[Lang]string{
CHINESE: "chinese",
ENGLISH: "english",
CHINESE_TRADITIONAL: "chinese_traditional",
}
// String ...
func (l Lang) String() string {
return langString[l]
}
// Err ...
type Err struct {
lang Lang
err map[Lang]map[Error]string
}
//var gOnce *sync.Once
var gErr *Err
func init() {
var tmpLang = make(map[Lang]map[Error]string)
gErr = &Err{err: tmpLang}
gErr.lang = CHINESE
gErr.Register(gErr.Default())
}
// NewErr ...
func NewErr() *Err {
return gErr
}
// SetLang ...
func (e *Err) SetLang(l Lang) {
e.lang = l
}
// GetLang ...
func (e *Err) GetLang() Lang {
return e.lang
}
// Register ...
func (e *Err) Register(err map[Error]string) {
e.err[e.GetLang()] = err
}
// Get ...
func (e *Err) Get(err Error) string {
return e.err[e.GetLang()][err]
}
// GetErr ...
func GetErr(err Error, args ...interface{}) error {
var argreal string
if len(args) > 0 {
argreal = fmt.Sprint(":", args)
}
return errors.New(fmt.Sprint(
NewErr().
Get(err),
argreal))
}