Skip to content

Commit 31ab71c

Browse files
gzioloclaude
andcommitted
Connectors: Improve error handling in AI client integration.
- Change catch blocks from `\Error` to `Exception` (global namespace). - Add `_doing_it_wrong` when an unregistered provider ID is passed. - Add `wp_trigger_error` to surface exception messages in catch blocks. - Change `_wp_connectors_set_provider_api_key` to return `bool`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 81a4100 commit 31ab71c

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

src/wp-includes/connectors.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ function _wp_connectors_is_api_key_valid( string $key, string $provider_id ): ?b
6565
$registry = AiClient::defaultRegistry();
6666

6767
if ( ! $registry->hasProvider( $provider_id ) ) {
68+
_doing_it_wrong(
69+
__FUNCTION__,
70+
sprintf(
71+
/* translators: %s: AI provider ID. */
72+
__( 'The provider "%s" is not registered in the AI client registry.' ),
73+
$provider_id
74+
),
75+
'7.0.0'
76+
);
6877
return null;
6978
}
7079

@@ -74,7 +83,8 @@ function _wp_connectors_is_api_key_valid( string $key, string $provider_id ): ?b
7483
);
7584

7685
return $registry->isProviderConfigured( $provider_id );
77-
} catch ( \Error $e ) {
86+
} catch ( Exception $e ) {
87+
wp_trigger_error( __FUNCTION__, $e->getMessage() );
7888
return null;
7989
}
8090
}
@@ -87,21 +97,34 @@ function _wp_connectors_is_api_key_valid( string $key, string $provider_id ): ?b
8797
*
8898
* @param string $key The API key.
8999
* @param string $provider_id The WP AI client provider ID.
100+
* @return bool True if the key was set successfully, false otherwise.
90101
*/
91-
function _wp_connectors_set_provider_api_key( string $key, string $provider_id ): void {
102+
function _wp_connectors_set_provider_api_key( string $key, string $provider_id ): bool {
92103
try {
93104
$registry = AiClient::defaultRegistry();
94105

95106
if ( ! $registry->hasProvider( $provider_id ) ) {
96-
return;
107+
_doing_it_wrong(
108+
__FUNCTION__,
109+
sprintf(
110+
/* translators: %s: AI provider ID. */
111+
__( 'The provider "%s" is not registered in the AI client registry.' ),
112+
$provider_id
113+
),
114+
'7.0.0'
115+
);
116+
return false;
97117
}
98118

99119
$registry->setProviderRequestAuthentication(
100120
$provider_id,
101121
new ApiKeyRequestAuthentication( $key )
102122
);
103-
} catch ( \Error $e ) {
104-
// WP AI Client not available.
123+
124+
return true;
125+
} catch ( Exception $e ) {
126+
wp_trigger_error( __FUNCTION__, $e->getMessage() );
127+
return false;
105128
}
106129
}
107130

0 commit comments

Comments
 (0)