This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
143 lines (122 loc) · 2.98 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Copyright 2018 gd Author. All Rights Reserved.
* Author: Chuck1024
*/
package derror
import (
"fmt"
"net/http"
)
var (
RpcSuccess = 0
Success = 200
BadRequest = 400
Unauthorized = 401
Forbidden = 403
NotFound = 404
SystemError = 500
ParameterError = 600
DBError = 701
CacheError = 702
RpcTimeout = 10001
RpcOverflow = 10002
RpcInternalServerError = 10003
RpcInvalidParam = 10004
UnknownError = "unknown error"
ErrMap = map[int]string{
RpcSuccess: "ok",
Success: "ok",
BadRequest: "bad request",
Unauthorized: "Unauthorized",
Forbidden: "Forbidden",
NotFound: "not found",
SystemError: "system error",
ParameterError: "Parameter error",
DBError: "db error",
CacheError: "cache error",
RpcTimeout: "timeout error",
RpcOverflow: "overflow error",
RpcInternalServerError: "interval server error",
RpcInvalidParam: "invalid param",
}
)
func GetErrorType(code int) string {
t, ok := ErrMap[code]
if !ok {
t = UnknownError
}
return t
}
type CodeError struct {
errCode int
errType string
errMsg string
}
func (err *CodeError) Code() int {
return err.errCode
}
func (err *CodeError) Type() string {
return err.errType
}
func (err *CodeError) Error() string {
return err.errMsg
}
func (err *CodeError) Detail() string {
if err.errCode == Success || err.errCode == RpcSuccess {
return err.Error()
} else {
return fmt.Sprintf("Type: %s, Error: %s", err.Type(), err.Error())
}
}
func (err *CodeError) String() string {
if err.errCode == Success || err.errCode == RpcSuccess {
return fmt.Sprintf("Code: %d, Type: %s, Info: %s", err.Code(), err.Type(), err.Error())
} else {
return fmt.Sprintf("Code: %d, Type: %s, Error: %s", err.Code(), err.Type(), err.Error())
}
}
func (err *CodeError) ToString() []byte {
return []byte(fmt.Sprintf("MaeError[%s]", err.Detail()))
}
func (err *CodeError) SetMsg(msg string) *CodeError {
err.errMsg = msg
return err
}
func SetCodeType(code int, errType string) *CodeError {
err := &CodeError{
errCode: code,
errType: errType,
}
return err
}
func MakeHttpErrorByStatusCode(statusCode int) *CodeError {
statusText := http.StatusText(statusCode)
err := &CodeError{
errCode: statusCode,
errType: statusText,
errMsg: statusText,
}
return err
}
func NewCodeError(code int, format string, args ...interface{}) *CodeError {
eType := GetErrorType(code)
msg := format
if len(format) > 0 && len(args) > 0 {
msg = fmt.Sprintf(format, args...)
}
err := &CodeError{
errCode: code,
errType: eType,
errMsg: msg,
}
return err
}
func MakeCodeError(code int, e error) *CodeError {
eType := GetErrorType(code)
err := &CodeError{
errCode: code,
errType: eType,
errMsg: e.Error(),
}
return err
}