fix(auth): 包装数据库错误为 AppError 避免裸 500
- repo.Create 错误包装为 CREATE_USER_FAILED - FindByEmail 非 RecordNotFound 错误包装为 QUERY_USER_FAILED - 修复无效 UUID tenant 导致 gorm 原生错误泄露为 500 的问题
This commit is contained in:
parent
3ba93870dd
commit
22dba67920
@ -9,6 +9,7 @@ import (
|
|||||||
"mengstack/internal/modules/auth/domain"
|
"mengstack/internal/modules/auth/domain"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
@ -35,6 +36,8 @@ func (s *Service) Register(ctx context.Context, req domain.RegisterRequest) (dom
|
|||||||
|
|
||||||
if _, err := s.repo.FindByEmail(ctx, tenantID, req.Email); err == nil {
|
if _, err := s.repo.FindByEmail(ctx, tenantID, req.Email); err == nil {
|
||||||
return domain.TokenPair{}, errors.ErrUserExists
|
return domain.TokenPair{}, errors.ErrUserExists
|
||||||
|
} else if err != gorm.ErrRecordNotFound {
|
||||||
|
return domain.TokenPair{}, errors.Wrap(err, "QUERY_USER_FAILED", "failed to check existing user", 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), 12)
|
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), 12)
|
||||||
@ -52,7 +55,7 @@ func (s *Service) Register(ctx context.Context, req domain.RegisterRequest) (dom
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := s.repo.Create(ctx, user); err != nil {
|
if err := s.repo.Create(ctx, user); err != nil {
|
||||||
return domain.TokenPair{}, err
|
return domain.TokenPair{}, errors.Wrap(err, "CREATE_USER_FAILED", "failed to create user", 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.generateTokens(user)
|
return s.generateTokens(user)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user