-
Notifications
You must be signed in to change notification settings - Fork 24
Implement Exception Hierarchy For Better Error Handling #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
@felixarntz This implements the exception hierarchy you suggested in PR 2 in the wp-ai-client review. Now developers can catch all AI Client exceptions with catch (AiClientExceptionInterface $e) for unified error handling, while still having specific exception types for different error categories (network, request, validation). The implementation is minimal and focused - exactly what was requested for better WP-ai-client integration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ref34t Thank you for getting this started!
A few points of feedback below, and one higher-level point here: I think we should put the exceptions into specific directories. For example:
InvalidArgumentException
andRuntimeException
intosrc/Common/Exception
NetworkException
andRequestException
intosrc/Providers/Http/Exception
(where we already haveResponseException
)
@JasonTheAdams It would be great to get your review as well - including regarding my own feedback.
I agree with @felixarntz! I like the direction, but we need to see these exceptions being used in a few places to indicate their concrete usage. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @[email protected]. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@felixarntz @JasonTheAdams Architectural Reorganization Moved exceptions to proper directories:
Concrete Usage Examples
Proper Inheritance Fixed inheritance relationships:
Enhanced Developer Experience (Static factory methods suggestion) "Simply adding static from* methods to the various *Exception classes" RequestException (3 methods):
ResponseException (4 methods):
NetworkException (5 methods):
Kindly review and let me know if you have any concerned |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @Ref34t! I'm not sure I see the point in creating our own InvalidArgumentException
and RuntimeException
classes. Do you, @felixarntz?
@JasonTheAdams I do actually. It's simply about providing classes that can implement a common interface to identify "this is a specific exception from our PHP AI Client SDK". Consuming code can then decide whether they want to catch e.g. |
I like that, @felixarntz! Admittedly, I hadn't noticed the common Interface. I like that idea of being able to catch exceptions thrown by this package. As a note, I think we should update the AGENTS.md to include a note that all exceptions should use our own, including primitives, and if a primitive doesn't exist it should be created. Not sure if we should add this elsewhere, too. If we're going down this road it's important that we're consistent. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tracking now, @Ref34t! Good work!
I don't think we need all these static methods. As far as I can tell, there all used only one time. If that's the case, the abstraction isn't necessary and we can just let the implementing code determine the code, message, and such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ref34t @JasonTheAdams I do think some of the static methods are useful - just not all of them.
Per my previous feedback, @Ref34t it would be great if you could review the codebase for where exceptions are thrown and update these exceptions to use the new exception classes and new static methods if applicable.
We should not introduce any exception classes or any static methods that aren't used anywhere - I think that's a good guiding principle. Therefore it's important that we update any existing exceptions to use the new approach, and if there are none for a specific exception or static method, we know it's not useful, at least not at this point.
very interesting Convo Champs @felixarntz @JasonTheAdams 🥇 |
…and PromptBuilder now use project-scoped InvalidArgumentException and RuntimeException for better error categorization and unified exception handling
…hp and HTTP Request/Response DTOs now use project-scoped exception classes for unified error handling
…romBadResponse() based on real usage in ResponseUtil, remove unused static methods keeping only those actually used in codebase
722138b
to
f901fce
Compare
|
The remaining files that were updated
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks for updating all those, @Ref34t! I left a couple comments and questions.
* | ||
* @since n.e.x.t | ||
*/ | ||
class NetworkException extends RuntimeException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should implement \Psr\Http\Client\NetworkExceptionInterface
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll accept this suggestion! Implemented all three PSR-18 interfaces with getRequest() methods and updated HttpTransporter
accordingly
resolved in 04e767f
* | ||
* @since n.e.x.t | ||
*/ | ||
class RequestException extends InvalidArgumentException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, should implement \Psr\Http\Client\RequestExceptionInterface
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved 04e767f
* @since 0.1.0 | ||
*/ | ||
class ResponseException extends Exception | ||
class ResponseException extends RuntimeException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar, should this implement the PSR-18 ClientExceptionInterface
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved 04e767f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ref34t This looks really close now. A few last points of feedback.
@@ -0,0 +1,19 @@ | |||
<?php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should probably live in Common\Contracts
, or in Common\Exception\Contracts
- not in its own separate top-level namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved here a6bbaf2
throw new RuntimeException( | ||
'Unexpected API response: Missing the data key.' | ||
); | ||
throw ResponseException::fromMissingData('OpenAI', 'data'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same change still needs to be made in the AnthropicModelMetadataDirectory
and in GoogleModelMetadataDirectory
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved here 7c742aa
// Check for 400 Bad Request responses indicating invalid request data | ||
if ($psr7Response->getStatusCode() === 400) { | ||
$body = (string) $psr7Response->getBody(); | ||
$errorDetail = $body ? substr($body, 0, 200) : 'Invalid request parameters'; | ||
throw RequestException::fromBadRequest($psr7Request, $errorDetail); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be handled here, because we want to make sure the caller of HttpTransporter::send
is in full control over what to do with the response.
Therefore, this should be moved to ResponseUtil::throwIfNotSuccessful
, so the caller can decide to use it, or not.
} | ||
|
||
throw new ResponseException($errorMessage, $response->getStatusCode()); | ||
throw ResponseException::fromBadResponse($response); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that we may be want to move the fromBadResponse
method from ResponseException
to RequestException
? Technically, the lines are blurry, but I think I have a good reason:
The only thing I personally consider a response exception is when something in the response itself is wrong (e.g. missing an expected field in a successful response), which should almost never happen. Everything else IMO is more suitable in RequestException
- because if the response has an error status code, we can consider the request failed, not the response.
Would love to get your perspective on this too @JasonTheAdams!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So in frameworks like Symfony they distinguish this a bit differently, they use:
HttpExceptionInterface
, which indicates a 4xx or 5xx response errorClientExceptionInterface
, which extendsHttpExceptionInterface
and is specific to 4xx response errorsServerExceptionInterface
, which extendsHttpExceptionInterface
and is specific to 5xx response errorsTransportExceptionInterface
, which means there was a network failure
So they don't actually distinguish it in terms of "response" and "request", I think because of the ambiguity being described here. So if we like that convention better we could go that route.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to doing it that way, however none of the exceptions you mention actually does what our ResponseException
here is currently covering: It's the (very likely only hypothetical) scenario where a response does not include the data we expect. This can only happen if e.g. OpenAI made a breaking change to its API response shape tomorrow and we didn't know about it. So very unlikely to ever run into it, but possible - however it's only relevant if the API response itself came back successfully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point. I'm good with RequestException
, or if we want something a touch more granular: UnsupportedRequestException
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! @felixarntz and I chatted a bit and landed on this suggestion:
RequestException
- base class and thrown within ProvidersRedirectException extends RequestException
- thrown for 3xx responsesClientException extends RequestException
- thrown for 4xx responsesServerException extends RequestException
- thrown for 5xx responsesNetworkException extends RequestException
- thrown for network errors
Sound good to you, @Ref34t? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the final outcome you decided upon. Will try to conclude it 👍
* @param string $context Additional context about where the field was expected. | ||
* @return self | ||
*/ | ||
public static function fromMissingData(string $apiName, string $fieldName, string $context = ''): self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several other places where this should be used:
- All exceptions thrown in
AbstractOpenAiCompatibleTextGenerationModel::parseResponseToGenerativeAiResult
- All exceptions thrown in
AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceToCandidate
- All exceptions thrown in
AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceMessageParts
- All exceptions thrown in
AbstractOpenAiCompatibleImageGenerationModel::parseResponseToGenerativeAiResult
- All exceptions thrown in
AbstractOpenAiCompatibleImageGenerationModel::parseResponseChoiceToCandidate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some of these where it's about something more specific than a missing field, we could have another public static function fromInvalidData(string $apiName, string $message): self
here.
Summary
Implements exception hierarchy to improve error handling across the WordPress AI Client ecosystem, directly addressing feedback from PR 2 in https://github.com/WordPress/wp-ai-client
Changes
New Exception Classes
Updated Code
Benefits
Unified Exception Handling:
Better Error Categorization:
Related
Addresses feedback from 2 review comments about improving exception handling for wp-ai-client integration. https://github.com/WordPress/wp-ai-client