php-tenant/Exceptions/FeatureNotFoundException.php
Claude 2601392b8d
refactor: create EntitlementException hierarchy with subtypes
Create exception subclasses for fine-grained error handling:
- LimitExceededException: feature usage limit exceeded
- PackageNotFoundException: package code not found during provisioning
- FeatureNotFoundException: feature code not found during checks
- PackageSuspendedException: workspace packages suspended

Update EntitlementService:
- Add canOrFail() and canForNamespaceOrFail() throwing variants
- Replace firstOrFail() with explicit PackageNotFoundException in provisioning
- Import new exception types, remove unused ModelNotFoundException

Update docs/entitlements.md with Exception Hierarchy section, API reference
entries for new methods, and updated Best Practices examples.

Fixes #19

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 13:30:48 +00:00

53 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Tenant\Exceptions;
/**
* Exception thrown when a referenced feature does not exist.
*
* This exception is thrown when an entitlement check references a feature
* code that is not defined in the features table.
*
* @see EntitlementException Base exception class
*/
class FeatureNotFoundException extends EntitlementException
{
public function __construct(
string $message = 'The requested feature was not found.',
?string $featureCode = null,
int $code = 404,
?\Throwable $previous = null
) {
parent::__construct($message, $featureCode, $code, $previous);
}
/**
* Create exception for a specific feature code.
*/
public static function forCode(string $featureCode): self
{
return new self(
message: "Feature '{$featureCode}' does not exist.",
featureCode: $featureCode,
);
}
/**
* Render the exception as an HTTP response.
*/
public function render($request)
{
if ($request->expectsJson()) {
return response()->json([
'message' => $this->getMessage(),
'error' => 'feature_not_found',
'feature_code' => $this->featureCode,
], $this->getCode());
}
return redirect()->back()
->with('error', $this->getMessage());
}
}