<?php

declare(strict_types=1);

namespace apexl\Io\exceptions;

use RuntimeException;
use Throwable;

/**
 * Thrown by {@see \apexl\Io\Traits\FindOr404Trait::findOr404()} when an
 * entity lookup returns null. {@see \apexl\Io\middleware\EntityNotFoundMiddleware}
 * catches it and converts to a 404 JSON response so controllers don't need
 * to write the find->null-guard->404 boilerplate by hand.
 */
final class EntityNotFoundException extends RuntimeException
{
    public function __construct(
        public readonly string $entityLabel,
        public readonly ?int   $entityId = null,
        ?Throwable             $previous = null,
    ) {
        $msg = $entityLabel . ' not found' . ($entityId !== null ? " (id={$entityId})" : '');
        parent::__construct($msg, 404, $previous);
    }
}
