fullstackhero/dotnet-starter-kit · error · NotFoundException

Parent category not found.

Error message

Parent category {parentId} not found.

What it means

CreateCategoryCommandHandler, when command.ParentCategoryId is supplied, verifies the parent exists with an AnyAsync check and throws NotFoundException (HTTP 404) if it does not; the category is not created. Only tenant-visible, non-deleted parent rows count.

Solutions

  1. Verify the parent category ID via the categories list endpoint
  2. Create the category without a ParentCategoryId (top-level) if no parent is needed
  3. Re-create the parent category first if it was deleted

Example fix

// before
new CreateCategoryCommand("Sub", null, unknownParentId);
// after
var parents = await mediator.Send(new SearchCategoriesQuery("Parent")); // pick a valid ID
new CreateCategoryCommand("Sub", null, parents.Items.First().Id);
Defensive patterns

Strategy: validation

Validate before calling

if (parentId is not null) {
  var parent = await mediator.Send(new GetCategoryByIdQuery(parentId)); // throws 404 if invalid
}

Try / catch

try { await mediator.Send(new CreateCategoryCommand(name, desc, parentId)); }
catch (NotFoundException) { /* parent gone — clear parent selector and retry top-level */ }

Prevention

When it happens

Trigger: Send/CreateCategory command with a non-null ParentCategoryId that matches no category in the current tenant.

Common situations: Client sends a stale parent ID after the parent was deleted; ID from another tenant; typo or swapped GUID fields in the request body.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of fullstackhero/dotnet-starter-kit@3f2959e683 (2026-09-15). Data as JSON: /api/errors/f93293977d6409ce. Report an issue: GitHub.

Appendix: source

Thrown at src/Modules/Catalog/Modules.Catalog/Features/v1/Categories/CreateCategory/CreateCategoryCommandHandler.cs:25

using Microsoft.EntityFrameworkCore;

namespace FSH.Modules.Catalog.Features.v1.Categories.CreateCategory;

public sealed class CreateCategoryCommandHandler(CatalogDbContext dbContext)
    : ICommandHandler<CreateCategoryCommand, Guid>
{
    public async ValueTask<Guid> Handle(CreateCategoryCommand command, CancellationToken cancellationToken)
    {
        ArgumentNullException.ThrowIfNull(command);

        if (command.ParentCategoryId is { } parentId)
        {
            bool parentExists = await dbContext.Categories
                .AnyAsync(c => c.Id == parentId, cancellationToken)
                .ConfigureAwait(false);
            if (!parentExists)
            {
                throw new NotFoundException($"Parent category {parentId} not found.");
            }
        }

        var category = Category.Create(command.Name, command.Description, command.ParentCategoryId);

        bool slugTaken = await dbContext.Categories
            .AnyAsync(c => c.Slug == category.Slug, cancellationToken)
            .ConfigureAwait(false);
        if (slugTaken)
        {
            throw new CustomException(
                $"A category with name '{command.Name}' already exists.",
                (IEnumerable<string>?)null,
                HttpStatusCode.Conflict);
        }

        dbContext.Categories.Add(category);
        await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

View on GitHub (pinned to 3f2959e683)