Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
19b0b50
Implement simplified exception hierarchy for better error handling
Sep 1, 2025
f1b23d3
Fix code style violations
Sep 1, 2025
a22c94e
Implement exception hierarchy reorganization with static factory methods
Sep 2, 2025
98a84cf
Fix code style violations in exception classes
Sep 2, 2025
28ce137
Fix line length violations in exception factory methods
Sep 2, 2025
227498b
Fix function declaration brace placement
Sep 2, 2025
eb9686d
Update @since tags from 0.2.0 to n.e.x.t
Sep 5, 2025
f158e71
Update core files to use custom exception classes - ProviderRegistry …
Sep 5, 2025
9eac85b
Update HTTP and File DTOs to use custom exceptions - Files/DTO/File.p…
Sep 5, 2025
1f2fc0e
Implement usage-driven exception hierarchy - Add ResponseException::f…
Sep 5, 2025
c1aede5
Add exception handling requirements to coding standards
Sep 5, 2025
f901fce
Implement usage-driven exception hierarchy with custom classes
Sep 5, 2025
048d889
Fix File exception handling to use specific MimeType exceptions
Sep 5, 2025
499b5ab
Complete exception hierarchy implementation across entire codebase
Sep 5, 2025
469f46e
Fix accidentally removed try-catch for unrecognized file extensions
Sep 10, 2025
c0d3b74
Update test to expect correct fallback error message for unknown exte…
Sep 10, 2025
04e767f
Implement PSR-18 exception interfaces for HTTP client compliance
Sep 10, 2025
d2f5d62
Generalize exception handling guidelines in AGENTS.md
Sep 10, 2025
9330edd
Fix code style violations in HTTP exception classes
Sep 10, 2025
34fec79
Merge remote-tracking branch 'origin/trunk' into feature/exception-hi…
Sep 11, 2025
5fe7df9
Restore missing ModelRequirements methods from trunk
Sep 11, 2025
23ceb28
Fix code style: add missing newline at end of file
Sep 11, 2025
a6bbaf2
Implement exception hierarchy improvements
Sep 11, 2025
7c742aa
Update Anthropic and Google ModelMetadataDirectory to use ResponseExc…
Sep 11, 2025
4a69461
Implement ClientException for 4xx responses in ResponseUtil
Sep 11, 2025
8ab52c3
Implement complete exception hierarchy with ClientException and Serve…
Sep 11, 2025
beb17e1
Update tests to expect new exception hierarchy
Sep 11, 2025
44c8a4f
Enhance ResponseException with semantic factory methods for parsing e…
Sep 11, 2025
4314134
Build centralized error message extraction utility
Sep 25, 2025
0f18425
Implement semantic exception hierarchy with proper inheritance patterns
Sep 25, 2025
2867cbc
Update exception classes to use centralized error extraction and Requ…
Sep 25, 2025
e16fba4
Update exception tests for new hierarchy structure
Sep 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ The production code in `src` follows a structured namespace hierarchy under the

All parameters, return values, and properties must use explicit type hints, except in cases where providing the correct type hint would be impossible given limitations of backward compatibility with PHP 7.4. In any case, concrete type annotations using PHPStan should be present.

### Exception handling

All exceptions must use the project's custom exception classes rather than PHP built-in exceptions. This includes:

- Use `WordPress\AiClient\Common\Exception\InvalidArgumentException` instead of PHP's `\InvalidArgumentException`
- Use `WordPress\AiClient\Common\Exception\RuntimeException` instead of PHP's `\RuntimeException`
- All custom exceptions implement `WordPress\AiClient\Exceptions\AiClientExceptionInterface` for unified exception handling
- Follow usage-driven design: only implement static factory methods that are actually used in the codebase

### Naming conventions

The following naming conventions must be followed for consistency and autoloading:
Expand Down
6 changes: 4 additions & 2 deletions src/AiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace WordPress\AiClient;

use WordPress\AiClient\Builders\PromptBuilder;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\ProviderImplementations\Anthropic\AnthropicProvider;
use WordPress\AiClient\ProviderImplementations\Google\GoogleProvider;
use WordPress\AiClient\ProviderImplementations\OpenAi\OpenAiProvider;
Expand Down Expand Up @@ -282,7 +284,7 @@ public static function generateSpeechResult(
*/
public static function message(?string $text = null)
{
throw new \RuntimeException(
throw new RuntimeException(
'MessageBuilder is not yet available. This method depends on builder infrastructure. ' .
'Use direct generation methods (generateTextResult, generateImageResult, etc.) for now.'
);
Expand All @@ -302,7 +304,7 @@ private static function validateModelOrConfigParameter($modelOrConfig): void
&& !$modelOrConfig instanceof ModelInterface
&& !$modelOrConfig instanceof ModelConfig
) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'Parameter must be a ModelInterface instance (specific model), ' .
'ModelConfig instance (for auto-discovery), or null (default auto-discovery). ' .
sprintf('Received: %s', is_object($modelOrConfig) ? get_class($modelOrConfig) : gettype($modelOrConfig))
Expand Down
4 changes: 2 additions & 2 deletions src/Builders/PromptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace WordPress\AiClient\Builders;

use InvalidArgumentException;
use RuntimeException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Files\DTO\File;
use WordPress\AiClient\Files\Enums\FileTypeEnum;
use WordPress\AiClient\Messages\DTO\Message;
Expand Down
2 changes: 1 addition & 1 deletion src/Common/AbstractDataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace WordPress\AiClient\Common;

use InvalidArgumentException;
use JsonSerializable;
use stdClass;
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;

/**
* Abstract base class for all Data Value Objects in the AI Client.
Expand Down
4 changes: 2 additions & 2 deletions src/Common/AbstractEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace WordPress\AiClient\Common;

use BadMethodCallException;
use InvalidArgumentException;
use JsonSerializable;
use ReflectionClass;
use RuntimeException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;

/**
* Abstract base class for enum-like behavior in PHP 7.4.
Expand Down
19 changes: 19 additions & 0 deletions src/Common/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Common\Exception;

use WordPress\AiClient\Exceptions\AiClientExceptionInterface;

/**
* Exception thrown when an invalid argument is provided.
*
* This extends PHP's built-in InvalidArgumentException while implementing
* the AI Client exception interface for consistent catch handling.
*
* @since n.e.x.t
*/
class InvalidArgumentException extends \InvalidArgumentException implements AiClientExceptionInterface
{
}
19 changes: 19 additions & 0 deletions src/Common/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Common\Exception;

use WordPress\AiClient\Exceptions\AiClientExceptionInterface;

/**
* Exception thrown for runtime errors.
*
* This extends PHP's built-in RuntimeException while implementing
* the AI Client exception interface for consistent catch handling.
*
* @since n.e.x.t
*/
class RuntimeException extends \RuntimeException implements AiClientExceptionInterface
{
}
19 changes: 19 additions & 0 deletions src/Exceptions/AiClientExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Exceptions;

use Throwable;

/**
* Base interface for all AI Client exceptions.
*
* This interface allows callers to catch all AI Client specific exceptions
* with a single catch statement.
*
* @since n.e.x.t
*/
interface AiClientExceptionInterface extends Throwable
{
}
11 changes: 3 additions & 8 deletions src/Files/DTO/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace WordPress\AiClient\Files\DTO;

use InvalidArgumentException;
use RuntimeException;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Files\Enums\FileTypeEnum;
use WordPress\AiClient\Files\ValueObjects\MimeType;

Expand Down Expand Up @@ -375,12 +375,7 @@ private function determineMimeType(

$extension = pathinfo($cleanPath, PATHINFO_EXTENSION);
if (!empty($extension)) {
try {
return MimeType::fromExtension($extension);
} catch (InvalidArgumentException $e) {
// Extension not recognized, continue to error
unset($e);
}
return MimeType::fromExtension($extension);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Files/ValueObjects/MimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\Files\ValueObjects;

use InvalidArgumentException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;

/**
* Value object representing a MIME type.
Expand Down
4 changes: 2 additions & 2 deletions src/Messages/DTO/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace WordPress\AiClient\Messages\DTO;

use InvalidArgumentException;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;

/**
Expand Down Expand Up @@ -188,7 +188,7 @@ final public static function fromArray(array $array): self
return new ModelMessage($parts);
} else {
// Only USER and MODEL roles are supported
throw new \InvalidArgumentException('Invalid message role: ' . $role->value);
throw new InvalidArgumentException('Invalid message role: ' . $role->value);
}
}
}
4 changes: 2 additions & 2 deletions src/Messages/DTO/MessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace WordPress\AiClient\Messages\DTO;

use InvalidArgumentException;
use RuntimeException;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Files\DTO\File;
use WordPress\AiClient\Messages\Enums\MessagePartChannelEnum;
use WordPress\AiClient\Messages\Enums\MessagePartTypeEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\ProviderImplementations\Anthropic;

use RuntimeException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Messages\Enums\ModalityEnum;
use WordPress\AiClient\Providers\Http\Contracts\RequestAuthenticationInterface;
use WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\ProviderImplementations\Anthropic;

use RuntimeException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\AbstractProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\ProviderImplementations\Google;

use RuntimeException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Files\Enums\FileTypeEnum;
use WordPress\AiClient\Files\Enums\MediaOrientationEnum;
use WordPress\AiClient\Messages\Enums\ModalityEnum;
Expand Down
2 changes: 1 addition & 1 deletion src/ProviderImplementations/Google/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\ProviderImplementations\Google;

use RuntimeException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\AbstractProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace WordPress\AiClient\ProviderImplementations\OpenAi;

use RuntimeException;
use WordPress\AiClient\Files\Enums\FileTypeEnum;
use WordPress\AiClient\Files\Enums\MediaOrientationEnum;
use WordPress\AiClient\Messages\Enums\ModalityEnum;
use WordPress\AiClient\Providers\Http\DTO\Request;
use WordPress\AiClient\Providers\Http\DTO\Response;
use WordPress\AiClient\Providers\Http\Enums\HttpMethodEnum;
use WordPress\AiClient\Providers\Http\Exception\ResponseException;
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
use WordPress\AiClient\Providers\Models\DTO\SupportedOption;
use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum;
Expand Down Expand Up @@ -53,9 +53,7 @@ protected function parseResponseToModelMetadataList(Response $response): array
/** @var ModelsResponseData $responseData */
$responseData = $response->getData();
if (!isset($responseData['data']) || !$responseData['data']) {
throw new RuntimeException(
'Unexpected API response: Missing the data key.'
);
throw ResponseException::fromMissingData('OpenAI', 'data');
}

// Unfortunately, the OpenAI API does not return model capabilities, so we have to hardcode them here.
Expand Down
2 changes: 1 addition & 1 deletion src/ProviderImplementations/OpenAi/OpenAiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\ProviderImplementations\OpenAi;

use RuntimeException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\AbstractProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\Providers\ApiBasedImplementation;

use InvalidArgumentException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
use WordPress\AiClient\Providers\Http\Contracts\WithHttpTransporterInterface;
use WordPress\AiClient\Providers\Http\Contracts\WithRequestAuthenticationInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\Providers\Contracts;

use InvalidArgumentException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\Providers\Contracts;

use InvalidArgumentException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\DTO\ProviderMetadata;
use WordPress\AiClient\Providers\Models\Contracts\ModelInterface;
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WordPress\AiClient\Providers\Contracts;

use InvalidArgumentException;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Operations\Contracts\OperationInterface;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/DTO/ProviderModelsMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace WordPress\AiClient\Providers\DTO;

use InvalidArgumentException;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Http/DTO/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace WordPress\AiClient\Providers\Http\DTO;

use InvalidArgumentException;
use JsonException;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\Http\Collections\HeadersCollection;
use WordPress\AiClient\Providers\Http\Enums\HttpMethodEnum;

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Http/DTO/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace WordPress\AiClient\Providers\Http\DTO;

use InvalidArgumentException;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Providers\Http\Collections\HeadersCollection;

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Providers/Http/Exception/NetworkException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Http\Exception;

use WordPress\AiClient\Common\Exception\RuntimeException;

/**
* Exception thrown for network-related errors.
*
* This includes HTTP transport errors, connection failures,
* timeouts, and other network-related issues.
*
* @since n.e.x.t
*/
class NetworkException extends RuntimeException
{
/**
* Creates a NetworkException from a PSR-18 network exception.
*
* @since n.e.x.t
*
* @param string $uri The URI that was being requested.
* @param \Throwable $networkException The PSR-18 network exception.
* @return self
*/
public static function fromPsr18NetworkException(string $uri, \Throwable $networkException): self
{
$message = sprintf(
'Network error occurred while sending request to %s: %s',
$uri,
$networkException->getMessage()
);

return new self($message, 0, $networkException);
}
}
Loading