修复数据库波动导致后台掉线
This commit is contained in:
@@ -3,6 +3,7 @@ package adminauth
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/modules/auth"
|
||||
)
|
||||
@@ -23,6 +24,8 @@ type TokenValidationError struct {
|
||||
Reason string
|
||||
TokenVersion int64
|
||||
CurrentVersion int64
|
||||
Detail string
|
||||
Unavailable bool
|
||||
err error
|
||||
}
|
||||
|
||||
@@ -44,10 +47,33 @@ func (e *TokenValidationError) AuthFailureVersions() (int64, int64) {
|
||||
return e.TokenVersion, e.CurrentVersion
|
||||
}
|
||||
|
||||
// AuthFailureDetail 返回仅供服务端日志使用的依赖错误摘要,不包含令牌内容。
|
||||
func (e *TokenValidationError) AuthFailureDetail() string {
|
||||
return e.Detail
|
||||
}
|
||||
|
||||
// AuthFailureUnavailable 标识当前失败是否由临时依赖故障引起。
|
||||
func (e *TokenValidationError) AuthFailureUnavailable() bool {
|
||||
return e.Unavailable
|
||||
}
|
||||
|
||||
func newTokenValidationError(reason string, err error) error {
|
||||
return &TokenValidationError{Reason: reason, err: err}
|
||||
}
|
||||
|
||||
func newTokenDependencyError(err error) error {
|
||||
detail := strings.Join(strings.Fields(err.Error()), " ")
|
||||
if len(detail) > 240 {
|
||||
detail = detail[:240]
|
||||
}
|
||||
return &TokenValidationError{
|
||||
Reason: "admin_db_query_failed",
|
||||
Detail: detail,
|
||||
Unavailable: true,
|
||||
err: errors.Join(ErrDependencyUnavailable, err),
|
||||
}
|
||||
}
|
||||
|
||||
func newTokenVersionMismatchError(tokenVersion, currentVersion int64) error {
|
||||
return &TokenValidationError{
|
||||
Reason: "token_version_mismatch",
|
||||
@@ -79,6 +105,9 @@ func (s *Service) Refresh(ctx context.Context, refreshToken string) (*auth.Token
|
||||
}
|
||||
admin, err := s.repo.FindActiveForToken(ctx, claims.UserID, claims.TokenVersion)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrDependencyUnavailable) {
|
||||
return nil, err
|
||||
}
|
||||
if errors.Is(err, ErrAdminDisabled) {
|
||||
return nil, ErrAdminDisabled
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user