Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: store return nil on principal entity non-existence (#307)
spinningbot636d7c1261f0
diff --git a/server/activity_manager.go b/server/activity_manager.goindex ce36cec013..9de2a4c46f 100644--- a/server/activity_manager.go+++ b/server/activity_manager.go@@ -84,6 +84,9 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activiif err != nil {return nil, fmt.Errorf("failed to find updater for posting webhook event after changing the issue status: %v, error: %w", meta.issue.Name, err)}+ if updater == nil {+ return nil, fmt.Errorf("Updater principal not found for ID %v", create.CreatorID)+ }// Call external webhook endpoint in Go routine to avoid blocking web serveing thread.go func() {@@ -163,6 +166,14 @@ func (m *ActivityManager) getWebhookContext(ctx context.Context, activity *api.Azap.Error(err))return webhookCtx, err}+ if oldAssignee == nil {+ err := fmt.Errorf("Failed to post webhook event after changing the issue assignee, old assignee not found for ID %v", oldID)+ m.s.l.Warn(err.Error(),+ zap.String("issue_name", meta.issue.Name),+ zap.String("old_assignee_id", update.OldValue),+ zap.Error(err))+ return webhookCtx, err+ }}if update.NewValue != "" {@@ -185,6 +196,14 @@ func (m *ActivityManager) getWebhookContext(ctx context.Context, activity *api.Azap.Error(err))return webhookCtx, err}+ if newAssignee == nil {+ err := fmt.Errorf("Failed to post webhook event after changing the issue assignee, new assignee not found for ID %v", newID)+ m.s.l.Warn(err.Error(),+ zap.String("issue_name", meta.issue.Name),+ zap.String("new_assignee_id", update.NewValue),+ zap.Error(err))+ return webhookCtx, err+ }if oldAssignee != nil && newAssignee != nil {title = fmt.Sprintf("Reassigned issue from %s to %s", oldAssignee.Name, newAssignee.Name)diff --git a/server/auth.go b/server/auth.goindex 2434bed844..0442e426f0 100644--- a/server/auth.go+++ b/server/auth.go@@ -26,11 +26,11 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {}user, err := s.PrincipalService.FindPrincipal(ctx, principalFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found: %s", login.Email))- }return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)}+ if user == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found: %s", login.Email))+ }memberFind := &api.MemberFind{PrincipalID: &user.ID,diff --git a/server/jwt.go b/server/jwt.goindex aa4c391500..23a33a815d 100644--- a/server/jwt.go+++ b/server/jwt.go@@ -224,11 +224,11 @@ func JWTMiddleware(l *zap.Logger, p api.PrincipalService, next echo.HandlerFunc,}user, err := p.FindPrincipal(ctx, principalFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find user ID: %d", principalID))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find user ID: %d", principalID)).SetInternal(err)}+ if user == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find user ID: %d", principalID))+ }if generateToken {generateTokenFunc := func() error {diff --git a/server/member.go b/server/member.goindex 414171f69f..ad4cbb8fbf 100644--- a/server/member.go+++ b/server/member.go@@ -38,11 +38,11 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {}user, err := s.PrincipalService.FindPrincipal(ctx, principalFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find user ID: %d", member.PrincipalID))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find user ID: %d", member.PrincipalID)).SetInternal(err)}+ if user == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find user ID: %d", member.PrincipalID))+ }bytes, err := json.Marshal(api.ActivityMemberCreatePayload{PrincipalID: member.PrincipalID,@@ -140,11 +140,11 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {}user, err := s.PrincipalService.FindPrincipal(ctx, principalFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find user ID: %d", updatedMember.PrincipalID))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find user ID: %d", updatedMember.PrincipalID)).SetInternal(err)}+ if user == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find user ID: %d", updatedMember.PrincipalID))+ }if memberPatch.Role != nil {bytes, err := json.Marshal(api.ActivityMemberRoleUpdatePayload{diff --git a/server/principal.go b/server/principal.goindex 992cd3b5f6..6ee92d3ce7 100644--- a/server/principal.go+++ b/server/principal.go@@ -150,6 +150,9 @@ func (s *Server) composePrincipalByID(ctx context.Context, id int) (*api.Principif err != nil {return nil, err}+ if id > 0 && principal == nil {+ return nil, fmt.Errorf("principal not found for ID %v", id)+ }if err = s.composePrincipalRole(ctx, principal); err != nil {return nil, errdiff --git a/store/principal.go b/store/principal.goindex 644cfe2bd8..ef4ab63a0d 100644--- a/store/principal.go+++ b/store/principal.go@@ -76,7 +76,6 @@ func (s *PrincipalService) FindPrincipalList(ctx context.Context) ([]*api.Princi}// FindPrincipal retrieves a principal based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *PrincipalService) FindPrincipal(ctx context.Context, find *api.PrincipalFind) (*api.Principal, error) {if find.ID != nil {@@ -99,12 +98,13 @@ func (s *PrincipalService) FindPrincipal(ctx context.Context, find *api.Principalist, err := findPrincipalList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("principal not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d principals with filter %+v, expect 1", len(list), find)}}-if err := s.cache.UpsertCache(api.PrincipalCache, list[0].ID, list[0]); err != nil {return nil, err}