Unverified Commit 17a68fc4 authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Improve group update error handling (#721)

parent ca367e49
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr

### Fixed
- Fix optimistic email imports when linking social profiles.
- Fix error on API group update name already taken.

## [3.8.0] - 2021-10-15
### Added
+9 −6
Original line number Diff line number Diff line
@@ -132,17 +132,20 @@ func (s *ApiServer) UpdateGroup(ctx context.Context, in *api.UpdateGroupRequest)
		}
	}

	err = UpdateGroup(ctx, s.logger, s.db, groupID, userID, uuid.Nil, in.GetName(), in.GetLangTag(), in.GetDescription(), in.GetAvatarUrl(), nil, in.GetOpen(), -1)
	if err != nil {
		if err == runtime.ErrGroupPermissionDenied {
	if err = UpdateGroup(ctx, s.logger, s.db, groupID, userID, uuid.Nil, in.GetName(), in.GetLangTag(), in.GetDescription(), in.GetAvatarUrl(), nil, in.GetOpen(), -1); err != nil {
		switch err {
		case runtime.ErrGroupPermissionDenied:
			return nil, status.Error(codes.NotFound, "Group not found or you're not allowed to update.")
		} else if err == runtime.ErrGroupNoUpdateOps {
		case runtime.ErrGroupNoUpdateOps:
			return nil, status.Error(codes.InvalidArgument, "Specify at least one field to update.")
		} else if err == runtime.ErrGroupNotUpdated {
		case runtime.ErrGroupNotUpdated:
			return nil, status.Error(codes.InvalidArgument, "No new fields in group update.")
		}
		case runtime.ErrGroupNameInUse:
			return nil, status.Error(codes.InvalidArgument, "Group name is in use.")
		default:
			return nil, status.Error(codes.Internal, "Error while trying to update group.")
		}
	}

	// After hook.
	if fn := s.runtime.AfterUpdateGroup(); fn != nil {
+0 −5
Original line number Diff line number Diff line
@@ -120,11 +120,6 @@ RETURNING id, creator_id, name, description, avatar_url, state, edge_count, lang

		groups, err := groupConvertRows(rows, 1)
		if err != nil {
			var pgErr *pgconn.PgError
			if errors.As(err, &pgErr) && pgErr.Code == dbErrorUniqueViolation {
				logger.Info("Could not create group as it already exists.", zap.String("name", name))
				return runtime.ErrGroupNameInUse
			}
			logger.Debug("Could not parse rows.", zap.Error(err))
			return err
		}