Skip to content

Commit 5b78dcd

Browse files
committed
异常增加namespace
1 parent 7ff1086 commit 5b78dcd

File tree

13 files changed

+162
-88
lines changed

13 files changed

+162
-88
lines changed

exception/base.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type APIException interface {
88
ErrorCode() int
99
Meta() interface{}
1010
Is(code int) bool
11+
Namespace() string
1112
}
1213

1314
// WithAPIException 携带元信息的异常
@@ -16,20 +17,31 @@ type WithAPIException interface {
1617
WithMeta(m interface{}) APIException
1718
}
1819

20+
func newException(namespace Namespace, code int, reason, format string, a ...interface{}) *exception {
21+
return &exception{
22+
namespace: namespace,
23+
code: code,
24+
reason: reason,
25+
message: fmt.Sprintf(format, a...),
26+
}
27+
}
28+
1929
// APIException is impliment for api exception
2030
type exception struct {
21-
code int
22-
msg string
23-
meta interface{}
31+
namespace Namespace
32+
code int
33+
reason string
34+
message string
35+
meta interface{}
2436
}
2537

2638
func (e *exception) Error() string {
27-
return e.msg
39+
return e.message
2840
}
2941

3042
// Code exception's code, 如果code不存在返回-1
3143
func (e *exception) ErrorCode() int {
32-
return e.code
44+
return int(e.code)
3345
}
3446

3547
// WithMeta 携带一些额外信息
@@ -46,16 +58,17 @@ func (e *exception) Is(code int) bool {
4658
return e.ErrorCode() == code
4759
}
4860

61+
func (e *exception) Namespace() string {
62+
return e.namespace.String()
63+
}
64+
4965
// NewAPIException 创建一个API异常
5066
// 用于其他模块自定义异常
51-
func NewAPIException(code int, format string, a ...interface{}) WithAPIException {
67+
func NewAPIException(code int, reason, format string, a ...interface{}) WithAPIException {
5268
// 0表示正常状态, 但是要排除变量的零值
5369
if code == 0 {
5470
code = -1
5571
}
5672

57-
return &exception{
58-
msg: fmt.Sprintf(format, a...),
59-
code: code,
60-
}
73+
return newException(usedNamespace, code, reason, format, a...)
6174
}

exception/code.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package exception
33
import "net/http"
44

55
const (
6+
// TokenExpired token过期
67
TokenExpired = 1000
78

89
// 1xx - 5xx copy from http status code
@@ -14,3 +15,24 @@ const (
1415

1516
UnKnownException = 9999
1617
)
18+
19+
var (
20+
reasonMap = map[int]string{
21+
Unauthorized: "认证失败",
22+
NotFound: "资源为找到",
23+
BadRequest: "请求不合法",
24+
InternalServerError: "系统内部错误",
25+
Forbidden: "访问未授权",
26+
UnKnownException: "未知异常",
27+
TokenExpired: "访问过期, 请重新访问",
28+
}
29+
)
30+
31+
func codeReason(code int) string {
32+
v, ok := reasonMap[code]
33+
if !ok {
34+
v = reasonMap[UnKnownException]
35+
}
36+
37+
return v
38+
}

exception/exception.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,33 @@
11
package exception
22

3-
import "fmt"
4-
53
// NewUnauthorized 未认证
64
func NewUnauthorized(format string, a ...interface{}) APIException {
7-
return &exception{
8-
msg: fmt.Sprintf(format, a...),
9-
code: Unauthorized,
10-
}
5+
return newException(usedNamespace, Unauthorized, codeReason(Unauthorized), format, a...)
116
}
127

138
// NewPermissionDeny 没有权限访问
149
func NewPermissionDeny(format string, a ...interface{}) APIException {
15-
return &exception{
16-
msg: fmt.Sprintf(format, a...),
17-
code: Forbidden,
18-
}
10+
return newException(usedNamespace, Forbidden, codeReason(Forbidden), format, a...)
1911
}
2012

2113
// NewTokenExpired token过期
2214
func NewTokenExpired(format string, a ...interface{}) APIException {
23-
return &exception{
24-
msg: fmt.Sprintf(format, a...),
25-
code: TokenExpired,
26-
}
15+
return newException(usedNamespace, TokenExpired, codeReason(TokenExpired), format, a...)
2716
}
2817

2918
// NewBadRequest todo
3019
func NewBadRequest(format string, a ...interface{}) APIException {
31-
return &exception{
32-
msg: fmt.Sprintf(format, a...),
33-
code: BadRequest,
34-
}
20+
return newException(usedNamespace, BadRequest, codeReason(BadRequest), format, a...)
3521
}
3622

3723
// NewNotFound todo
3824
func NewNotFound(format string, a ...interface{}) APIException {
39-
return &exception{
40-
msg: fmt.Sprintf(format, a...),
41-
code: NotFound,
42-
}
25+
return newException(usedNamespace, NotFound, codeReason(BadRequest), format, a...)
4326
}
4427

4528
// NewInternalServerError 500
4629
func NewInternalServerError(format string, a ...interface{}) APIException {
47-
return &exception{
48-
msg: fmt.Sprintf(format, a...),
49-
code: InternalServerError,
50-
}
30+
return newException(usedNamespace, InternalServerError, codeReason(InternalServerError), format, a...)
5131
}
5232

5333
// IsNotFoundError 判断是否是NotFoundError

exception/namespace.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package exception
2+
3+
var (
4+
usedNamespace = GlobalNamespace
5+
)
6+
7+
// Namespace 异常所属空间
8+
type Namespace string
9+
10+
func (ns Namespace) String() string {
11+
return string(ns)
12+
}
13+
14+
const (
15+
// GlobalNamespace 所有服务公用的一些异常
16+
GlobalNamespace = Namespace("global")
17+
// Keyauth 权限系统, 每个子系统有自己的异常空间
18+
Keyauth = Namespace("keyauth")
19+
)
20+
21+
// ChangeNameSapce 切换异常空间
22+
func ChangeNameSapce(ns Namespace) {
23+
usedNamespace = ns
24+
}

go.mod

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ require (
2323
github.com/julienschmidt/httprouter v1.3.0
2424
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2525
github.com/modern-go/reflect2 v1.0.1 // indirect
26-
github.com/onsi/ginkgo v1.10.3 // indirect
27-
github.com/onsi/gomega v1.7.1 // indirect
26+
github.com/onsi/ginkgo v1.11.0 // indirect
27+
github.com/onsi/gomega v1.8.1 // indirect
2828
github.com/pkg/errors v0.8.1
29-
github.com/prometheus/client_golang v1.2.1 // indirect
29+
github.com/prometheus/client_golang v1.3.0 // indirect
3030
github.com/soheilhy/cmux v0.1.4 // indirect
3131
github.com/stretchr/testify v1.4.0
3232
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
3333
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
3434
go.etcd.io/bbolt v1.3.3 // indirect
35-
go.etcd.io/etcd v3.3.17+incompatible
36-
go.mongodb.org/mongo-driver v1.1.3
37-
go.uber.org/zap v1.12.0
38-
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a
39-
golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd
35+
go.etcd.io/etcd v3.3.18+incompatible
36+
go.mongodb.org/mongo-driver v1.2.0
37+
go.uber.org/zap v1.13.0
38+
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915
39+
golang.org/x/sys v0.0.0-20191220220014-0732a990476f
4040
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
41-
google.golang.org/grpc v1.25.1 // indirect
41+
google.golang.org/grpc v1.26.0 // indirect
4242
sigs.k8s.io/yaml v1.1.0 // indirect
4343
)

0 commit comments

Comments
 (0)