diff --git a/internal/app/gameplay_errors.go b/internal/app/gameplay_errors.go new file mode 100644 index 0000000..9bf6156 --- /dev/null +++ b/internal/app/gameplay_errors.go @@ -0,0 +1,20 @@ +package app + +import ( + "errors" + + "evening_detective_server/internal/services/gameplay_service" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func gameplayErrorStatus(err error, internalMessage string) error { + if errors.Is(err, gameplay_service.ErrTeamNotFound) { + return status.Error(codes.NotFound, gameplay_service.ErrTeamNotFound.Error()) + } + if errors.Is(err, gameplay_service.ErrGameNotFound) { + return status.Error(codes.NotFound, gameplay_service.ErrGameNotFound.Error()) + } + return status.Error(codes.Internal, internalMessage) +} diff --git a/internal/app/teams.go b/internal/app/teams.go index 093b5c3..e251756 100644 --- a/internal/app/teams.go +++ b/internal/app/teams.go @@ -105,9 +105,7 @@ func (s *server) GiveTeamApplications(ctx context.Context, req *proto.GiveTeamAp req.Name, ) if err != nil { - return &proto.GiveTeamApplicationsRsp{ - Error: err.Error(), - }, nil + return nil, gameplayErrorStatus(err, "не удалось обработать заявку команды") } return &proto.GiveTeamApplicationsRsp{}, nil @@ -120,9 +118,7 @@ func (s *server) GetTeamStory(ctx context.Context, req *proto.GetTeamStoryReq) ( req.Password, ) if err != nil { - return &proto.GetTeamStoryRsp{ - Error: err.Error(), - }, nil + return nil, gameplayErrorStatus(err, "не удалось получить историю команды") } protoActions := make([]*proto.TeamAction, 0, len(actions)) for _, action := range actions { @@ -145,9 +141,7 @@ func (s *server) AddTeamAction(ctx context.Context, req *proto.AddTeamActionReq) false, ) if err != nil { - return &proto.AddTeamActionRsp{ - Error: err.Error(), - }, nil + return nil, gameplayErrorStatus(err, "не удалось добавить действие команды") } return &proto.AddTeamActionRsp{}, nil @@ -167,9 +161,7 @@ func (s *server) DeleteLastTeamAction(ctx context.Context, req *proto.DeleteLast int(req.ID), ) if err != nil { - return &proto.DeleteLastTeamActionRsp{ - Error: err.Error(), - }, nil + return nil, gameplayErrorStatus(err, "не удалось удалить последнее действие команды") } return &proto.DeleteLastTeamActionRsp{}, nil diff --git a/internal/app/user_errors.go b/internal/app/user_errors.go index d23c07d..a3df9e4 100644 --- a/internal/app/user_errors.go +++ b/internal/app/user_errors.go @@ -3,7 +3,6 @@ package app import ( "errors" - "evening_detective_server/internal/repos/users_repo" "evening_detective_server/internal/services/users_service" "google.golang.org/grpc/codes" @@ -16,7 +15,7 @@ func userErrorStatus(err error, internalMessage string) error { return status.Error(codes.NotFound, err.Error()) case errors.Is(err, users_service.ErrTermsNotAccepted), errors.Is(err, users_service.ErrInvalidEmail), errors.Is(err, users_service.ErrInvalidRole), errors.Is(err, users_service.ErrInvalidPasswordChange): return status.Error(codes.InvalidArgument, err.Error()) - case errors.Is(err, users_service.ErrInvalidCredentials), errors.Is(err, users_repo.ErrUserNotFound): + case errors.Is(err, users_service.ErrInvalidCredentials): return status.Error(codes.Unauthenticated, users_service.ErrInvalidCredentials.Error()) case errors.Is(err, users_service.ErrInvalidAccountPassword): return status.Error(codes.PermissionDenied, err.Error()) diff --git a/internal/services/gameplay_service/service.go b/internal/services/gameplay_service/service.go index c777103..90f1b53 100644 --- a/internal/services/gameplay_service/service.go +++ b/internal/services/gameplay_service/service.go @@ -2,6 +2,7 @@ package gameplay_service import ( "context" + "errors" "evening_detective_server/internal/modules/cleaner" "evening_detective_server/internal/modules/storytelling" "evening_detective_server/internal/repos/actions_repo" @@ -10,6 +11,12 @@ import ( "evening_detective_server/internal/repos/teams_repo" "evening_detective_server/internal/services/game_model" "evening_detective_server/internal/services/scenarios_service" + "fmt" +) + +var ( + ErrTeamNotFound = errors.New("Команда не найдена") + ErrGameNotFound = errors.New("Игра не найдена") ) type GameplayService struct { @@ -46,22 +53,22 @@ func NewGameplayService( } func (s *GameplayService) GiveTeamApplications(ctx context.Context, teamID int, name string) error { - return s.applicationsRepo.UpdateApplicationStatus(ctx, teamID, name, "done") + return mapGameplayError(s.applicationsRepo.UpdateApplicationStatus(ctx, teamID, name, "done")) } func (s *GameplayService) GetTeamActions(ctx context.Context, teamID int, password string) (*storytelling.Story, *game_model.Game, []actions_repo.Action, error) { team, err := s.teamsRepo.GetTeamByID(ctx, teamID) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, mapGameplayError(err) } if team.Password != password || team.ParticipationStatus == "cancelled" { - return nil, nil, nil, teams_repo.ErrTeamNotFound + return nil, nil, nil, fmt.Errorf("%w: %v", ErrTeamNotFound, teams_repo.ErrTeamNotFound) } game, err := s.gamesRepo.GetGameByID(ctx, team.GameID) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, mapGameplayError(err) } if game.Status == "finish" { return &storytelling.Story{}, game_model.MapGame(game, s.domain), nil, nil @@ -69,12 +76,12 @@ func (s *GameplayService) GetTeamActions(ctx context.Context, teamID int, passwo actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, mapGameplayError(err) } scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, mapGameplayError(err) } codes := make([]string, len(actions)) @@ -89,36 +96,36 @@ func (s *GameplayService) GetTeamActions(ctx context.Context, teamID int, passwo func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, password string, actionCode string, viaMCP bool) error { team, err := s.teamsRepo.GetTeamByID(ctx, teamID) if err != nil { - return err + return mapGameplayError(err) } if team.Password != password || team.ParticipationStatus == "cancelled" { - return teams_repo.ErrTeamNotFound + return fmt.Errorf("%w: %v", ErrTeamNotFound, teams_repo.ErrTeamNotFound) } _, err = s.actionsRepo.AddAction(ctx, teamID, actionCode, viaMCP) if err != nil { - return err + return mapGameplayError(err) } if team.ParticipationStatus == "pending" { if err := s.teamsRepo.ConfirmParticipationByFirstAction(ctx, teamID); err != nil { - return err + return mapGameplayError(err) } } game, err := s.gamesRepo.GetGameByID(ctx, team.GameID) if err != nil { - return err + return mapGameplayError(err) } actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID) if err != nil { - return err + return mapGameplayError(err) } scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID) if err != nil { - return err + return mapGameplayError(err) } actions := make([]string, len(actionDetails)) @@ -133,7 +140,7 @@ func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, passwor app := newApplication(application, s.domain) app.Name = s.cleaner.ClearText(app.Name) if _, err := s.applicationsRepo.AddApplication(ctx, teamID, app.Name, app.Image, app.FileType); err != nil { - return err + return mapGameplayError(err) } } @@ -143,22 +150,22 @@ func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, passwor func (s *GameplayService) DeleteLastTeamAction(ctx context.Context, teamID int) error { team, err := s.teamsRepo.GetTeamByID(ctx, teamID) if err != nil { - return err + return mapGameplayError(err) } game, err := s.gamesRepo.GetGameByID(ctx, team.GameID) if err != nil { - return err + return mapGameplayError(err) } actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID) if err != nil { - return err + return mapGameplayError(err) } scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID) if err != nil { - return err + return mapGameplayError(err) } actions := make([]string, len(actionDetails)) @@ -171,9 +178,19 @@ func (s *GameplayService) DeleteLastTeamAction(ctx context.Context, teamID int) for _, application := range lastPlace.Applications { if err := s.applicationsRepo.DeleteApplicationByTeamIDAndName(ctx, teamID, application.Name); err != nil { - return err + return mapGameplayError(err) } } - return s.actionsRepo.DeleteLastActionByTeamID(ctx, teamID) + return mapGameplayError(s.actionsRepo.DeleteLastActionByTeamID(ctx, teamID)) +} + +func mapGameplayError(err error) error { + if errors.Is(err, teams_repo.ErrTeamNotFound) { + return fmt.Errorf("%w: %v", ErrTeamNotFound, err) + } + if errors.Is(err, games_repo.ErrGameNotFound) { + return fmt.Errorf("%w: %v", ErrGameNotFound, err) + } + return err } diff --git a/internal/services/users_service/service.go b/internal/services/users_service/service.go index 28abaad..6ee3729 100644 --- a/internal/services/users_service/service.go +++ b/internal/services/users_service/service.go @@ -264,7 +264,7 @@ func (s *UsersService) Login( ) if err != nil { if errors.Is(err, users_repo.ErrUserNotFound) { - return "", "", err + return "", "", fmt.Errorf("%w: %w", ErrInvalidCredentials, err) } return "", "", err }