Skip to content

Commit

Permalink
Fix minor bugs and improve error handling in generating content infra…
Browse files Browse the repository at this point in the history
…structure.
  • Loading branch information
felixarntz committed Aug 27, 2024
1 parent 5326c6e commit e2a3cf9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function handle_request( WP_REST_Request $request ): WP_REST_Response

$service = $this->services_api->get_available_service( $request['slug'] );

if ( isset( $request['model'] ) ) {
if ( isset( $request['model'] ) && '' !== $request['model'] ) {
$model = $request['model'];
} else {
// For now, we just use the first model available. TODO: Improve this later, e.g. by specifying a default.
Expand Down Expand Up @@ -180,8 +180,9 @@ protected function handle_request( WP_REST_Request $request ): WP_REST_Response
throw REST_Exception::create(
'rest_generating_content_failed',
sprintf(
/* translators: %s: original error message */
esc_html__( 'Generating content failed: %s', 'wp-starter-plugin' ),
/* translators: 1: model slug, 2: original error message */
esc_html__( 'Generating content with model %1$s failed: %2$s', 'wp-starter-plugin' ),
esc_html( $model->get_model_slug() ),
esc_html( $e->getMessage() )
),
500
Expand All @@ -190,8 +191,9 @@ protected function handle_request( WP_REST_Request $request ): WP_REST_Response
throw REST_Exception::create(
'rest_invalid_content',
sprintf(
/* translators: %s: original error message */
esc_html__( 'Invalid content provided: %s', 'wp-starter-plugin' ),
/* translators: 1: model slug, 2: original error message */
esc_html__( 'Invalid content provided to model %1$s: %2$s', 'wp-starter-plugin' ),
esc_html( $model->get_model_slug() ),
esc_html( $e->getMessage() )
),
400
Expand Down
22 changes: 13 additions & 9 deletions src/ai-store/generative-ai-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ export class GenerativeAiService {
}
}

return await apiFetch( {
path: `/wp-starter-plugin/v1/services/${ this.slug }:generate-content`,
method: 'POST',
data: {
content,
model: model || '',
modelParams: modelParams || {},
},
} );
try {
return await apiFetch( {
path: `/wp-starter-plugin/v1/services/${ this.slug }:generate-content`,
method: 'POST',
data: {
content,
model: model || '',
modelParams: modelParams || {},
},
} );
} catch ( error ) {
throw new Error( error.message || error.code || error );
}
}
}

Expand Down

0 comments on commit e2a3cf9

Please sign in to comment.