Skip to content

Commit

Permalink
补充自定义异常
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Jan 11, 2024
1 parent 92aa862 commit 67dddd6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
32 changes: 16 additions & 16 deletions exception/bussiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,82 @@ func NewAPIExceptionFromError(err error) *APIException {

// NewUnauthorized 未认证
func NewUnauthorized(format string, a ...interface{}) *APIException {
return NewAPIException("", Unauthorized, codeReason(Unauthorized), format, a...)
return NewAPIException(Unauthorized, codeReason(Unauthorized), format, a...)
}

// NewPermissionDeny 没有权限访问
func NewPermissionDeny(format string, a ...interface{}) *APIException {
return NewAPIException("", Forbidden, codeReason(Forbidden), format, a...)
return NewAPIException(Forbidden, codeReason(Forbidden), format, a...)
}

// NewAccessTokenIllegal 访问token过期
func NewAccessTokenIllegal(format string, a ...interface{}) *APIException {
return NewAPIException("", AccessTokenIllegal, codeReason(AccessTokenIllegal), format, a...)
return NewAPIException(AccessTokenIllegal, codeReason(AccessTokenIllegal), format, a...)
}

// NewRefreshTokenIllegal 访问token过期
func NewRefreshTokenIllegal(format string, a ...interface{}) *APIException {
return NewAPIException("", RefreshTokenIllegal, codeReason(RefreshTokenIllegal), format, a...)
return NewAPIException(RefreshTokenIllegal, codeReason(RefreshTokenIllegal), format, a...)
}

// NewOtherClientsLoggedIn 其他端登录
func NewOtherClientsLoggedIn(format string, a ...interface{}) *APIException {
return NewAPIException("", OtherClientsLoggedIn, codeReason(OtherClientsLoggedIn), format, a...)
return NewAPIException(OtherClientsLoggedIn, codeReason(OtherClientsLoggedIn), format, a...)
}

// NewOtherPlaceLoggedIn 异地登录
func NewOtherPlaceLoggedIn(format string, a ...interface{}) *APIException {
return NewAPIException("", OtherPlaceLoggedIn, codeReason(OtherPlaceLoggedIn), format, a...)
return NewAPIException(OtherPlaceLoggedIn, codeReason(OtherPlaceLoggedIn), format, a...)
}

// NewOtherIPLoggedIn 异常IP登录
func NewOtherIPLoggedIn(format string, a ...interface{}) *APIException {
return NewAPIException("", OtherIPLoggedIn, codeReason(OtherIPLoggedIn), format, a...)
return NewAPIException(OtherIPLoggedIn, codeReason(OtherIPLoggedIn), format, a...)
}

// NewSessionTerminated 会话结束
func NewSessionTerminated(format string, a ...interface{}) *APIException {
return NewAPIException("", SessionTerminated, codeReason(SessionTerminated), format, a...)
return NewAPIException(SessionTerminated, codeReason(SessionTerminated), format, a...)
}

// NewAccessTokenExpired 访问token过期
func NewAccessTokenExpired(format string, a ...interface{}) *APIException {
return NewAPIException("", AccessTokenExpired, codeReason(SessionTerminated), format, a...)
return NewAPIException(AccessTokenExpired, codeReason(SessionTerminated), format, a...)
}

// NewRefreshTokenExpired 刷新token过期
func NewRefreshTokenExpired(format string, a ...interface{}) *APIException {
return NewAPIException("", RefreshTokenExpired, codeReason(RefreshTokenExpired), format, a...)
return NewAPIException(RefreshTokenExpired, codeReason(RefreshTokenExpired), format, a...)
}

// NewBadRequest todo
func NewBadRequest(format string, a ...interface{}) *APIException {
return NewAPIException("", BadRequest, codeReason(BadRequest), format, a...)
return NewAPIException(BadRequest, codeReason(BadRequest), format, a...)
}

// NewNotFound todo
func NewNotFound(format string, a ...interface{}) *APIException {
return NewAPIException("", NotFound, codeReason(NotFound), format, a...)
return NewAPIException(NotFound, codeReason(NotFound), format, a...)
}

// NewConflict todo
func NewConflict(format string, a ...interface{}) *APIException {
return NewAPIException("", Conflict, codeReason(Conflict), format, a...)
return NewAPIException(Conflict, codeReason(Conflict), format, a...)
}

// NewInternalServerError 500
func NewInternalServerError(format string, a ...interface{}) *APIException {
return NewAPIException("", InternalServerError, codeReason(InternalServerError), format, a...)
return NewAPIException(InternalServerError, codeReason(InternalServerError), format, a...)
}

// NewVerifyCodeRequiredError 50018
func NewVerifyCodeRequiredError(format string, a ...interface{}) *APIException {
return NewAPIException("", VerifyCodeRequired, codeReason(VerifyCodeRequired), format, a...)
return NewAPIException(VerifyCodeRequired, codeReason(VerifyCodeRequired), format, a...)
}

// NewPasswordExired 50019
func NewPasswordExired(format string, a ...interface{}) *APIException {
return NewAPIException("", PasswordExired, codeReason(PasswordExired), format, a...)
return NewAPIException(PasswordExired, codeReason(PasswordExired), format, a...)
}

// IsNotFoundError 判断是否是NotFoundError
Expand Down
17 changes: 9 additions & 8 deletions exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (

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

return &APIException{
Namespace: namespace,
ErrCode: code,
Reason: reason,
HttpCode: httpCode,
Message: fmt.Sprintf(format, a...),
ErrCode: code,
Reason: reason,
HttpCode: httpCode,
Message: fmt.Sprintf(format, a...),
}
}

Expand Down Expand Up @@ -79,8 +78,9 @@ func (e *APIException) ErrorCode() int {
return int(e.ErrCode)
}

func (e *APIException) WithHttpCode(httpCode int) {
func (e *APIException) WithHttpCode(httpCode int) *APIException {
e.HttpCode = httpCode
return e
}

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

func (e *APIException) WithNamespace(ns string) {
func (e *APIException) WithNamespace(ns string) *APIException {
e.Namespace = ns
return e
}
2 changes: 1 addition & 1 deletion grpc/gcontext/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func NewExceptionFromTrailer(md metadata.MD, err error) *exception.APIException
if message == "" {
message = err.Error()
}
return exception.NewAPIException(Namespace, code, reason, message)
return exception.NewAPIException(code, reason, message).WithNamespace(Namespace)
}
2 changes: 1 addition & 1 deletion http/response/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetDataFromBody(body io.ReadCloser, v interface{}) error {
}

if *data.Code != 0 {
return exception.NewAPIException(data.Namespace, *data.Code, data.Reason, data.Message)
return exception.NewAPIException(*data.Code, data.Reason, data.Message).WithNamespace(data.Namespace)
}

return nil
Expand Down

0 comments on commit 67dddd6

Please sign in to comment.