errorCode = $errorCode; $this->httpStatusCode = $httpStatusCode; $this->context = $context; } /** * Get the error code */ public function getErrorCode(): string { return $this->errorCode; } /** * Get the HTTP status code */ public function getHttpStatusCode(): int { return $this->httpStatusCode; } /** * Get additional context */ public function getContext(): array { return $this->context; } /** * Create from API response */ public static function fromResponse(WhatsappApiResponse $response): static { return new static( message: $response->getErrorMessage() ?? 'Unknown API error', errorCode: $response->error ?? 'api_error', httpStatusCode: $response->statusCode, context: $response->raw ); } /** * Create insufficient credits exception */ public static function insufficientCredits(int $available, int $needed): static { return new static( message: "Insufficient credits. Available: {$available}, Needed: {$needed}", errorCode: 'insufficient_credits', httpStatusCode: 402, context: [ 'credits_available' => $available, 'credits_needed' => $needed ] ); } /** * Create unauthorized exception */ public static function unauthorized(): static { return new static( message: 'Invalid or missing API token', errorCode: 'unauthorized', httpStatusCode: 401 ); } /** * Create template not found exception */ public static function templateNotFound(string $template, string $language): static { return new static( message: "Template '{$template}' with language '{$language}' not found", errorCode: 'template_not_found', httpStatusCode: 404, context: [ 'template' => $template, 'language' => $language ] ); } /** * Create service not configured exception */ public static function serviceNotConfigured(): static { return new static( message: 'WhatsApp service is not configured. Please check your settings.', errorCode: 'service_not_configured', httpStatusCode: 0 ); } /** * Create connection failed exception */ public static function connectionFailed(string $reason = ''): static { return new static( message: 'Failed to connect to WhatsApp API' . ($reason ? ": {$reason}" : ''), errorCode: 'connection_failed', httpStatusCode: 0, context: ['reason' => $reason] ); } }