From 22dba67920b148cfa2f7719cbf2e9f17587dd172 Mon Sep 17 00:00:00 2001 From: MengStack Dev Date: Sat, 3 Oct 2026 00:35:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E5=8C=85=E8=A3=85=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E9=94=99=E8=AF=AF=E4=B8=BA=20AppError=20?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E8=A3=B8=20500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - repo.Create 错误包装为 CREATE_USER_FAILED - FindByEmail 非 RecordNotFound 错误包装为 QUERY_USER_FAILED - 修复无效 UUID tenant 导致 gorm 原生错误泄露为 500 的问题 --- internal/modules/auth/application/service.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/modules/auth/application/service.go b/internal/modules/auth/application/service.go index 4b51e15..155c46d 100644 --- a/internal/modules/auth/application/service.go +++ b/internal/modules/auth/application/service.go @@ -9,6 +9,7 @@ import ( "mengstack/internal/modules/auth/domain" "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" ) 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 { 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) @@ -52,7 +55,7 @@ func (s *Service) Register(ctx context.Context, req domain.RegisterRequest) (dom } 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)