-
Notifications
You must be signed in to change notification settings - Fork 21
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.
src/Exceptions/NetworkException.php
Outdated
* | ||
* @since 0.2.0 | ||
*/ | ||
class NetworkException extends RuntimeException implements AiClientExceptionInterface |
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.
What is this exception class used for? We should only introduce exceptions that we have concrete usage for.
I can see how a NetworkException
might be useful, but then we should also make sure it's used where applicable.
src/Exceptions/RequestException.php
Outdated
* | ||
* @since 0.2.0 | ||
*/ | ||
class RequestException extends RuntimeException implements AiClientExceptionInterface |
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.
What would this class be used for? I'm thinking that RequestException
is most likely only relevant for bad request data (i.e. as an extension of InvalidArgumentException
).
Similarly to my other comment, we need to actually use it if we add it here. There are at least several places in the provider implementation code or its abstract base classes where this could be used.
I think this would also be good to throw in scenarios when a Response
comes back with a 400 Bad Request
status code. In that case it means an invalid argument was set that our code didn't catch, but the API did.
|
||
/** | ||
* Exception class for HTTP response errors. | ||
* | ||
* @since 0.1.0 | ||
*/ | ||
class ResponseException extends Exception | ||
class ResponseException extends RequestException |
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 shouldn't extend RequestException
- a ResponseException
is definitely something different.
I think the main use-case for ResponseException
is when the response data is unexpected - which should never happen, unless a provider changed in ways our code is not aware of. That's IMO the primary use-case for this.
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.
* This extends PHP's built-in InvalidArgumentException while implementing | ||
* the AI Client exception interface for consistent catch handling. | ||
* | ||
* @since 0.2.0 |
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.
Here and throughout, always use n.e.x.t
because we don't want to assume we know the version something will be released in.
* @since 0.2.0 | |
* @since n.e.x.t |
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 🥇 |
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