Skip to content

Commit 67dddd6

Browse files
committed
补充自定义异常
1 parent 92aa862 commit 67dddd6

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

exception/bussiness.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,82 @@ func NewAPIExceptionFromError(err error) *APIException {
77

88
// NewUnauthorized 未认证
99
func NewUnauthorized(format string, a ...interface{}) *APIException {
10-
return NewAPIException("", Unauthorized, codeReason(Unauthorized), format, a...)
10+
return NewAPIException(Unauthorized, codeReason(Unauthorized), format, a...)
1111
}
1212

1313
// NewPermissionDeny 没有权限访问
1414
func NewPermissionDeny(format string, a ...interface{}) *APIException {
15-
return NewAPIException("", Forbidden, codeReason(Forbidden), format, a...)
15+
return NewAPIException(Forbidden, codeReason(Forbidden), format, a...)
1616
}
1717

1818
// NewAccessTokenIllegal 访问token过期
1919
func NewAccessTokenIllegal(format string, a ...interface{}) *APIException {
20-
return NewAPIException("", AccessTokenIllegal, codeReason(AccessTokenIllegal), format, a...)
20+
return NewAPIException(AccessTokenIllegal, codeReason(AccessTokenIllegal), format, a...)
2121
}
2222

2323
// NewRefreshTokenIllegal 访问token过期
2424
func NewRefreshTokenIllegal(format string, a ...interface{}) *APIException {
25-
return NewAPIException("", RefreshTokenIllegal, codeReason(RefreshTokenIllegal), format, a...)
25+
return NewAPIException(RefreshTokenIllegal, codeReason(RefreshTokenIllegal), format, a...)
2626
}
2727

2828
// NewOtherClientsLoggedIn 其他端登录
2929
func NewOtherClientsLoggedIn(format string, a ...interface{}) *APIException {
30-
return NewAPIException("", OtherClientsLoggedIn, codeReason(OtherClientsLoggedIn), format, a...)
30+
return NewAPIException(OtherClientsLoggedIn, codeReason(OtherClientsLoggedIn), format, a...)
3131
}
3232

3333
// NewOtherPlaceLoggedIn 异地登录
3434
func NewOtherPlaceLoggedIn(format string, a ...interface{}) *APIException {
35-
return NewAPIException("", OtherPlaceLoggedIn, codeReason(OtherPlaceLoggedIn), format, a...)
35+
return NewAPIException(OtherPlaceLoggedIn, codeReason(OtherPlaceLoggedIn), format, a...)
3636
}
3737

3838
// NewOtherIPLoggedIn 异常IP登录
3939
func NewOtherIPLoggedIn(format string, a ...interface{}) *APIException {
40-
return NewAPIException("", OtherIPLoggedIn, codeReason(OtherIPLoggedIn), format, a...)
40+
return NewAPIException(OtherIPLoggedIn, codeReason(OtherIPLoggedIn), format, a...)
4141
}
4242

4343
// NewSessionTerminated 会话结束
4444
func NewSessionTerminated(format string, a ...interface{}) *APIException {
45-
return NewAPIException("", SessionTerminated, codeReason(SessionTerminated), format, a...)
45+
return NewAPIException(SessionTerminated, codeReason(SessionTerminated), format, a...)
4646
}
4747

4848
// NewAccessTokenExpired 访问token过期
4949
func NewAccessTokenExpired(format string, a ...interface{}) *APIException {
50-
return NewAPIException("", AccessTokenExpired, codeReason(SessionTerminated), format, a...)
50+
return NewAPIException(AccessTokenExpired, codeReason(SessionTerminated), format, a...)
5151
}
5252

5353
// NewRefreshTokenExpired 刷新token过期
5454
func NewRefreshTokenExpired(format string, a ...interface{}) *APIException {
55-
return NewAPIException("", RefreshTokenExpired, codeReason(RefreshTokenExpired), format, a...)
55+
return NewAPIException(RefreshTokenExpired, codeReason(RefreshTokenExpired), format, a...)
5656
}
5757

5858
// NewBadRequest todo
5959
func NewBadRequest(format string, a ...interface{}) *APIException {
60-
return NewAPIException("", BadRequest, codeReason(BadRequest), format, a...)
60+
return NewAPIException(BadRequest, codeReason(BadRequest), format, a...)
6161
}
6262

6363
// NewNotFound todo
6464
func NewNotFound(format string, a ...interface{}) *APIException {
65-
return NewAPIException("", NotFound, codeReason(NotFound), format, a...)
65+
return NewAPIException(NotFound, codeReason(NotFound), format, a...)
6666
}
6767

6868
// NewConflict todo
6969
func NewConflict(format string, a ...interface{}) *APIException {
70-
return NewAPIException("", Conflict, codeReason(Conflict), format, a...)
70+
return NewAPIException(Conflict, codeReason(Conflict), format, a...)
7171
}
7272

7373
// NewInternalServerError 500
7474
func NewInternalServerError(format string, a ...interface{}) *APIException {
75-
return NewAPIException("", InternalServerError, codeReason(InternalServerError), format, a...)
75+
return NewAPIException(InternalServerError, codeReason(InternalServerError), format, a...)
7676
}
7777

7878
// NewVerifyCodeRequiredError 50018
7979
func NewVerifyCodeRequiredError(format string, a ...interface{}) *APIException {
80-
return NewAPIException("", VerifyCodeRequired, codeReason(VerifyCodeRequired), format, a...)
80+
return NewAPIException(VerifyCodeRequired, codeReason(VerifyCodeRequired), format, a...)
8181
}
8282

8383
// NewPasswordExired 50019
8484
func NewPasswordExired(format string, a ...interface{}) *APIException {
85-
return NewAPIException("", PasswordExired, codeReason(PasswordExired), format, a...)
85+
return NewAPIException(PasswordExired, codeReason(PasswordExired), format, a...)
8686
}
8787

8888
// IsNotFoundError 判断是否是NotFoundError

exception/exception.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414

1515
// NewAPIException 创建一个API异常
1616
// 用于其他模块自定义异常
17-
func NewAPIException(namespace string, code int, reason, format string, a ...interface{}) *APIException {
17+
func NewAPIException(code int, reason, format string, a ...interface{}) *APIException {
1818
// 0表示正常状态, 但是要排除变量的零值
1919
if code == 0 {
2020
code = -1
@@ -28,11 +28,10 @@ func NewAPIException(namespace string, code int, reason, format string, a ...int
2828
}
2929

3030
return &APIException{
31-
Namespace: namespace,
32-
ErrCode: code,
33-
Reason: reason,
34-
HttpCode: httpCode,
35-
Message: fmt.Sprintf(format, a...),
31+
ErrCode: code,
32+
Reason: reason,
33+
HttpCode: httpCode,
34+
Message: fmt.Sprintf(format, a...),
3635
}
3736
}
3837

@@ -79,8 +78,9 @@ func (e *APIException) ErrorCode() int {
7978
return int(e.ErrCode)
8079
}
8180

82-
func (e *APIException) WithHttpCode(httpCode int) {
81+
func (e *APIException) WithHttpCode(httpCode int) *APIException {
8382
e.HttpCode = httpCode
83+
return e
8484
}
8585

8686
// Code exception's code, 如果code不存在返回-1
@@ -123,6 +123,7 @@ func (e *APIException) GetReason() string {
123123
return e.Reason
124124
}
125125

126-
func (e *APIException) WithNamespace(ns string) {
126+
func (e *APIException) WithNamespace(ns string) *APIException {
127127
e.Namespace = ns
128+
return e
128129
}

grpc/gcontext/exception.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ func NewExceptionFromTrailer(md metadata.MD, err error) *exception.APIException
3636
if message == "" {
3737
message = err.Error()
3838
}
39-
return exception.NewAPIException(Namespace, code, reason, message)
39+
return exception.NewAPIException(code, reason, message).WithNamespace(Namespace)
4040
}

http/response/receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func GetDataFromBody(body io.ReadCloser, v interface{}) error {
2727
}
2828

2929
if *data.Code != 0 {
30-
return exception.NewAPIException(data.Namespace, *data.Code, data.Reason, data.Message)
30+
return exception.NewAPIException(*data.Code, data.Reason, data.Message).WithNamespace(data.Namespace)
3131
}
3232

3333
return nil

0 commit comments

Comments
 (0)