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>
63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Core\Tenant\Exceptions;
|
|
|
|
/**
|
|
* Exception thrown when an operation targets a suspended package.
|
|
*
|
|
* This exception indicates that the workspace's packages have been suspended
|
|
* (e.g., for non-payment or policy violation), preventing feature access.
|
|
*
|
|
* @see EntitlementException Base exception class
|
|
*/
|
|
class PackageSuspendedException extends EntitlementException
|
|
{
|
|
public function __construct(
|
|
string $message = 'Your package has been suspended.',
|
|
?string $featureCode = null,
|
|
public readonly ?int $workspaceId = null,
|
|
int $code = 403,
|
|
?\Throwable $previous = null
|
|
) {
|
|
parent::__construct($message, $featureCode, $code, $previous);
|
|
}
|
|
|
|
/**
|
|
* Create exception for a specific workspace.
|
|
*/
|
|
public static function forWorkspace(int $workspaceId, ?string $featureCode = null): self
|
|
{
|
|
return new self(
|
|
message: 'Your package has been suspended. Please contact support or update your billing details.',
|
|
featureCode: $featureCode,
|
|
workspaceId: $workspaceId,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the workspace ID whose packages are suspended.
|
|
*/
|
|
public function getWorkspaceId(): ?int
|
|
{
|
|
return $this->workspaceId;
|
|
}
|
|
|
|
/**
|
|
* Render the exception as an HTTP response.
|
|
*/
|
|
public function render($request)
|
|
{
|
|
if ($request->expectsJson()) {
|
|
return response()->json([
|
|
'message' => $this->getMessage(),
|
|
'error' => 'package_suspended',
|
|
'feature_code' => $this->featureCode,
|
|
], $this->getCode());
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('error', $this->getMessage());
|
|
}
|
|
}
|