-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
51 lines (42 loc) · 1.36 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
package gors
import (
"fmt"
"github.com/go-leo/gox/convx"
"regexp"
)
var msgRegExp = regexp.MustCompile("^gors.Error, Code: (\\d+), Message: (.+)$")
// Status 代表业务状态
type Status struct {
Code int `json:"code,omitempty" yaml:"code,omitempty" xml:"code,omitempty" toml:"code,omitempty" codec:"code,omitempty" mapstructure:"code,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty" xml:"message,omitempty" toml:"message,omitempty" codec:"message,omitempty" mapstructure:"message,omitempty"`
}
// Error 代表http状态码和业务状态
type Error struct {
// StatusCode http status code
StatusCode int
// Code business service code
Code int
// Message error message
Message string
}
func (e Error) Error() string {
return fmt.Sprintf("gors.Error, Code: %d, Message: %s", e.Code, e.Message)
}
func (e Error) Status() *Status {
return &Status{Code: e.Code, Message: e.Message}
}
func ErrorFromMessage(msg string) (Error, bool) {
if !msgRegExp.MatchString(msg) {
return Error{}, false
}
subStrings := msgRegExp.FindAllStringSubmatch(msg, -1)
if len(subStrings) != 1 {
return Error{}, false
}
if len(subStrings[0]) != 3 {
return Error{}, false
}
return Error{Code: convx.ToInt(subStrings[0][1]), Message: subStrings[0][2]}, true
}
func errorValue() Error { return Error{} }
func errorPointer() *Error { return &Error{} }