php-tenant/Exceptions/PackageNotFoundException.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

61 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Tenant\Exceptions;
/**
* Exception thrown when a referenced package does not exist.
*
* This exception is thrown during provisioning when the requested package
* code cannot be found in the packages table.
*
* @see EntitlementException Base exception class
*/
class PackageNotFoundException extends EntitlementException
{
public function __construct(
string $message = 'The requested package was not found.',
public readonly ?string $packageCode = null,
int $code = 404,
?\Throwable $previous = null
) {
parent::__construct($message, featureCode: null, code: $code, previous: $previous);
}
/**
* Create exception for a specific package code.
*/
public static function forCode(string $packageCode): self
{
return new self(
message: "Package '{$packageCode}' not found.",
packageCode: $packageCode,
);
}
/**
* Get the package code that was not found.
*/
public function getPackageCode(): ?string
{
return $this->packageCode;
}
/**
* Render the exception as an HTTP response.
*/
public function render($request)
{
if ($request->expectsJson()) {
return response()->json([
'message' => $this->getMessage(),
'error' => 'package_not_found',
'package_code' => $this->packageCode,
], $this->getCode());
}
return redirect()->back()
->with('error', $this->getMessage());
}
}