Commit 18b2dc6f authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Better error handling for missing accounts.

parent 0926091a
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -29,6 +29,9 @@ func (s *ApiServer) GetAccount(ctx context.Context, in *empty.Empty) (*api.Accou

	user, err := GetAccount(s.logger, s.db, s.tracker, userID)
	if err != nil {
		if err == ErrAccountNotFound {
			return nil, status.Error(codes.NotFound, "Account not found.")
		}
		return nil, status.Error(codes.Internal, "Error retrieving user account.")
	}

+3 −0
Original line number Diff line number Diff line
@@ -86,6 +86,9 @@ func (s *ConsoleServer) ExportAccount(ctx context.Context, in *console.AccountId
	// Core user account.
	account, err := GetAccount(s.logger, s.db, nil, userID)
	if err != nil {
		if err == ErrAccountNotFound {
			return nil, status.Error(codes.NotFound, "Account not found.")
		}
		s.logger.Error("Could not export account data", zap.Error(err), zap.String("user_id", in.Id))
		return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
	}
+6 −2
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import (
	"go.uber.org/zap"
)

var ErrAccountNotFound = errors.New("account not found")

func GetAccount(logger *zap.Logger, db *sql.DB, tracker Tracker, userID uuid.UUID) (*api.Account, error) {
	var displayName sql.NullString
	var username sql.NullString
@@ -55,8 +57,10 @@ SELECT username, display_name, avatar_url, lang_tag, location, timezone, metadat
FROM users
WHERE id = $1`

	if err := db.QueryRow(query, userID).Scan(&username, &displayName, &avatarURL, &langTag, &locat, &timezone, &metadata,
		&wallet, &email, &facebook, &google, &gamecenter, &steam, &customID, &edge_count, &createTime, &updateTime, &verifyTime); err != nil {
	if err := db.QueryRow(query, userID).Scan(&username, &displayName, &avatarURL, &langTag, &locat, &timezone, &metadata, &wallet, &email, &facebook, &google, &gamecenter, &steam, &customID, &edge_count, &createTime, &updateTime, &verifyTime); err != nil {
		if err == sql.ErrNoRows {
			return nil, ErrAccountNotFound
		}
		logger.Error("Error retrieving user account.", zap.Error(err))
		return nil, err
	}