Tencent/WeKnora · warning

favorite resource id is required

Error message

favorite resource id is required

What it means

ErrFavoriteEmptyID is a sentinel error returned by the favorite service's Add and Remove operations when the resourceID argument is empty or whitespace-only. A favorite must reference a concrete resource, so the service rejects blank IDs before hitting the repository, keeping GORM errors out of the HTTP layer.

Source

Thrown at internal/application/service/user_resource_favorite.go:16

package service

import (
	"context"
	"errors"
	"strings"

	"github.com/Tencent/WeKnora/internal/types"
	"github.com/Tencent/WeKnora/internal/types/interfaces"
)

// Sentinel errors so the handler can map cleanly to HTTP status codes
// without leaking GORM internals.
var (
	ErrFavoriteInvalidType = errors.New("invalid favorite resource type")
	ErrFavoriteEmptyID     = errors.New("favorite resource id is required")
)

type userResourceFavoriteService struct {
	repo interfaces.UserResourceFavoriteRepository
}

// NewUserResourceFavoriteService wraps the repository with input
// validation (allowlist of resource types, non-empty resource id).
// We keep service thin on purpose — favoriting is a non-business action
// that doesn't need audit logging or cross-aggregate side effects.
func NewUserResourceFavoriteService(repo interfaces.UserResourceFavoriteRepository) interfaces.UserResourceFavoriteService {
	return &userResourceFavoriteService{repo: repo}
}

func (s *userResourceFavoriteService) List(
	ctx context.Context, userID string, tenantID uint64, resourceType string,
) ([]*types.UserResourceFavorite, error) {
	if !types.IsValidFavoriteResourceType(resourceType) {

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Ensure the caller supplies the resource ID; check request binding/route params.
  2. Validate the ID is non-empty at the handler layer and return 400 before invoking the service.
  3. If IDs are generated upstream, confirm the generating step actually ran (e.g. resource was created successfully).

Example fix

// before
id := req.ResourceID // may be ""
svc.AddFavorite(ctx, userID, tenantID, resourceType, id)
// after
if strings.TrimSpace(req.ResourceID) == "" {
    return httpError(400, "resource id is required")
}
svc.AddFavorite(ctx, userID, tenantID, resourceType, req.ResourceID)
Defensive patterns

Strategy: validation

Validate before calling

if strings.TrimSpace(resourceID) == "" {
    return httpError(400, "resource id is required")
}

Try / catch

// handler
if errors.Is(err, service.ErrFavoriteEmptyID) {
    return httpError(400, err.Error())
}

Prevention

When it happens

Trigger: Calling Add/AddFavorite or Remove/RemoveFavorite with resourceID == "" or only whitespace (e.g. an unset field in the request struct, a URL param that failed to bind).

Common situations: Client omits the resource id in the request body/path; router param name mismatch leaves the variable empty; upstream code passes a zero-value struct field.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/b1bdac1b2c7c3095. Report an issue: GitHub.