Skip to content

Commit 1563e86

Browse files
committed
添加token额外异常
1 parent 4030c73 commit 1563e86

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

exception/code.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package exception
33
import "net/http"
44

55
const (
6+
// AccessTokenIllegal 访问token不合法
7+
AccessTokenIllegal = 50008
8+
// RefreshTokenIllegal 刷新token不合法
9+
RefreshTokenIllegal = 50009
10+
// OtherClientsLoggedIn 启动端登录
11+
OtherClientsLoggedIn = 50012
612
// AccessTokenExpired token过期
7-
AccessTokenExpired = 1000
13+
AccessTokenExpired = 50014
814
// RefreshTokenExpired token过期
9-
RefreshTokenExpired = 1001
15+
RefreshTokenExpired = 50015
1016

1117
// 1xx - 5xx copy from http status code
1218
Unauthorized = http.StatusUnauthorized
@@ -15,19 +21,22 @@ const (
1521
Forbidden = http.StatusForbidden
1622
NotFound = http.StatusNotFound
1723

18-
UnKnownException = 9999
24+
UnKnownException = 99999
1925
)
2026

2127
var (
2228
reasonMap = map[int]string{
23-
Unauthorized: "认证失败",
24-
NotFound: "资源未找到",
25-
BadRequest: "请求不合法",
26-
InternalServerError: "系统内部错误",
27-
Forbidden: "访问未授权",
28-
UnKnownException: "未知异常",
29-
AccessTokenExpired: "访问过期, 请刷新",
30-
RefreshTokenExpired: "刷新过期, 请登录",
29+
Unauthorized: "认证失败",
30+
NotFound: "资源未找到",
31+
BadRequest: "请求不合法",
32+
InternalServerError: "系统内部错误",
33+
Forbidden: "访问未授权",
34+
UnKnownException: "未知异常",
35+
AccessTokenIllegal: "访问令牌不合法",
36+
RefreshTokenIllegal: "刷新令牌不合法",
37+
OtherClientsLoggedIn: "用户已经通过其他端登录",
38+
AccessTokenExpired: "访问过期, 请刷新",
39+
RefreshTokenExpired: "刷新过期, 请登录",
3140
}
3241
)
3342

exception/exception.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package exception
22

3+
import "fmt"
4+
35
// NewAPIException 创建一个API异常
46
// 用于其他模块自定义异常
57
func NewAPIException(namespace string, code int, reason, format string, a ...interface{}) WithAPIException {
@@ -8,7 +10,12 @@ func NewAPIException(namespace string, code int, reason, format string, a ...int
810
code = -1
911
}
1012

11-
return newException(Namespace(namespace), code, format, a...)
13+
return &exception{
14+
namespace: Namespace(namespace),
15+
code: code,
16+
reason: codeReason(code),
17+
message: fmt.Sprintf(format, a...),
18+
}
1219
}
1320

1421
// NewUnauthorized 未认证
@@ -21,6 +28,21 @@ func NewPermissionDeny(format string, a ...interface{}) APIException {
2128
return newException(usedNamespace, Forbidden, format, a...)
2229
}
2330

31+
// NewAccessTokenIllegal 访问token过期
32+
func NewAccessTokenIllegal(format string, a ...interface{}) APIException {
33+
return newException(usedNamespace, AccessTokenIllegal, format, a...)
34+
}
35+
36+
// NewRefreshTokenIllegal 访问token过期
37+
func NewRefreshTokenIllegal(format string, a ...interface{}) APIException {
38+
return newException(usedNamespace, RefreshTokenIllegal, format, a...)
39+
}
40+
41+
// NewOtherClientsLoggedIn 访问token过期
42+
func NewOtherClientsLoggedIn(format string, a ...interface{}) APIException {
43+
return newException(usedNamespace, OtherClientsLoggedIn, format, a...)
44+
}
45+
2446
// NewAccessTokenExpired 访问token过期
2547
func NewAccessTokenExpired(format string, a ...interface{}) APIException {
2648
return newException(usedNamespace, AccessTokenExpired, format, a...)

0 commit comments

Comments
 (0)