| 1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
-
- namespace App\Ai;
-
- use RuntimeException;
- use Throwable;
-
- /**
- * Anything that goes wrong while asking an AI model.
- */
- final class AiException extends RuntimeException
- {
- /** The app is not set up correctly, for example a missing API key. */
- public const string SETUP = 'setup';
-
- /** The provider failed: network, overload, rate limit, refusal. */
- public const string PROVIDER = 'provider';
-
- /** The AI answered, but the answer is not a usable store plan. */
- public const string INVALID_RESULT = 'invalid_result';
-
- public function __construct(string $message, public readonly string $reason = self::PROVIDER, ?Throwable $previous = null)
- {
- parent::__construct($message, 0, $previous);
- }
-
- /**
- * A short message that is safe to show to a merchant.
- */
- public function friendlyMessage(): string
- {
- return match ($this->reason) {
- self::SETUP => 'The AI service is not set up yet. Please contact support.',
- self::INVALID_RESULT => 'The AI returned a design we could not use. Please try again.',
- default => 'The AI service had a problem. Please try again in a moment.',
- };
- }
- }
|