throwError.go
1.5 KB
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
package lib
import "strings"
const (
INTERNAL_SERVER_ERROR = iota + 1
BAD_GATEWAY_ERROR
TRANSACTION_ERROR
ARG_ERROR
RES_NO_FIND_ERROR
BUSINESS_ERROR
)
type ServiceError struct {
Prefix string
Code int
Message string
}
func (serviceError ServiceError) Error() string {
return serviceError.Message
}
func ThrowError(serviceErrorNo int, attachMessages ...string) error {
switch serviceErrorNo {
case INTERNAL_SERVER_ERROR:
return ServiceError{
Prefix: "内部服务出错:",
Code: INTERNAL_SERVER_ERROR,
Message: strings.Join(attachMessages, ""),
}
case BAD_GATEWAY_ERROR:
return ServiceError{
Prefix: "服务网关错误:",
Code: BAD_GATEWAY_ERROR,
Message: strings.Join(attachMessages, ""),
}
case TRANSACTION_ERROR:
return ServiceError{
Prefix: "事务处理错误:",
Code: TRANSACTION_ERROR,
Message: strings.Join(attachMessages, ""),
}
case ARG_ERROR:
return ServiceError{
Prefix: "参数认证错误:",
Code: ARG_ERROR,
Message: strings.Join(attachMessages, ""),
}
case RES_NO_FIND_ERROR:
return ServiceError{
Prefix: "找不到相关资源:",
Code: RES_NO_FIND_ERROR,
Message: strings.Join(attachMessages, ""),
}
case BUSINESS_ERROR:
return ServiceError{
Prefix: "业务逻辑错误:",
Code: BUSINESS_ERROR,
Message: strings.Join(attachMessages, ""),
}
default:
return ServiceError{
Prefix: "未定义错误:",
Code: 500,
Message: strings.Join(attachMessages, ""),
}
}
}