Skip to content

Commit

Permalink
Added error handing for the text to speech feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Mar 29, 2024
1 parent f5fa87c commit 8aaa664
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
77 changes: 77 additions & 0 deletions includes/Classifai/Features/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function feature_setup() {
}

add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
add_action( 'admin_notices', [ $this, 'show_error_if' ] );
add_action( 'save_post', [ $this, 'save_post_metadata' ], 5 );
}

Expand Down Expand Up @@ -227,6 +228,18 @@ public function rest_handle_audio( \WP_Post $post, WP_REST_Request $request ) {

if ( $results && ! is_wp_error( $results ) ) {
$this->save( $results, $request->get_param( 'id' ) );
delete_post_meta( $post->ID, '_classifai_text_to_speech_error' );
} elseif ( is_wp_error( $results ) ) {
update_post_meta(
$post->ID,
'_classifai_text_to_speech_error',
wp_json_encode(
[
'code' => $results->get_error_code(),
'message' => $results->get_error_message(),
]
)
);
}
}
}
Expand All @@ -235,6 +248,19 @@ public function rest_handle_audio( \WP_Post $post, WP_REST_Request $request ) {
* Register any needed endpoints.
*/
public function register_endpoints() {
$post_types = $this->get_supported_post_types();
foreach ( $post_types as $post_type ) {
register_meta(
$post_type,
'_classifai_text_to_speech_error',
[
'show_in_rest' => true,
'single' => true,
'auth_callback' => '__return_true',
]
);
}

register_rest_route(
'classifai/v1',
'synthesize-speech/(?P<id>\d+)',
Expand Down Expand Up @@ -462,6 +488,18 @@ public function save_post_metadata( int $post_id ) {

if ( $results && ! is_wp_error( $results ) ) {
$this->save( $results, $post_id );
delete_post_meta( $post_id, '_classifai_text_to_speech_error' );
} elseif ( is_wp_error( $results ) ) {
update_post_meta(
$post_id,
'_classifai_text_to_speech_error',
wp_json_encode(
[
'code' => $results->get_error_code(),
'message' => $results->get_error_message(),
]
)
);
}
}
}
Expand Down Expand Up @@ -856,4 +894,43 @@ public function migrate_settings() {

return $new_settings;
}

/**
* Outputs an admin notice with the error message if needed.
*/
public function show_error_if() {
global $post;

if ( empty( $post ) ) {
return;
}

$post_id = $post->ID;

if ( empty( $post_id ) ) {
return;
}

$error = get_post_meta( $post_id, '_classifai_text_to_speech_error', true );

if ( ! empty( $error ) ) {
delete_post_meta( $post_id, '_classifai_text_to_speech_error' );
$error = (array) json_decode( $error );
$code = ! empty( $error['code'] ) ? $error['code'] : 500;
$message = ! empty( $error['message'] ) ? $error['message'] : 'Unknown API error';

?>
<div class="notice notice-error is-dismissible">
<p>
<?php esc_html_e( 'Error: Audio generation failed.', 'classifai' ); ?>
</p>
<p>
<?php echo esc_html( $code ); ?>
-
<?php echo esc_html( $message ); ?>
</p>
</div>
<?php
}
}
}
7 changes: 7 additions & 0 deletions includes/Classifai/Providers/AWS/AmazonPolly.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ public function synthesize_speech( int $post_id ) {
}
}

if ( mb_strlen( $post_content ) > 3000 ) {
return new WP_Error(
'aws_polly_length_error',
esc_html__( 'Maximum text length has been exceeded.', 'classifai' )
);
}

$voice = $settings[ static::ID ]['voice'] ?? '';

try {
Expand Down
32 changes: 31 additions & 1 deletion src/js/gutenberg-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-vars */
import { ReactComponent as icon } from '../../assets/img/block-icon.svg';
import { handleClick } from './helpers';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, subscribe } from '@wordpress/data';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import {
Button,
Expand Down Expand Up @@ -632,4 +632,34 @@ const ClassifAIPlugin = () => {
);
};

let saveHappened = false;
let showingNotice = false;

subscribe( () => {
if ( saveHappened === false ) {
saveHappened = wp.data.select( 'core/editor' ).isSavingPost() === true;
}

if (
saveHappened &&
wp.data.select( 'core/editor' ).isSavingPost() === false &&
showingNotice === false
) {
const meta = wp.data
.select( 'core/editor' )
.getCurrentPostAttribute( 'meta' );
if ( meta && meta._classifai_text_to_speech_error ) {
showingNotice = true;
const error = JSON.parse( meta._classifai_text_to_speech_error );
wp.data
.dispatch( 'core/notices' )
.createErrorNotice(
`Audio generation failed. Error: ${ error.code } - ${ error.message }`
);
saveHappened = false;
showingNotice = false;
}
}
} );

registerPlugin( 'classifai-plugin', { render: ClassifAIPlugin } );

0 comments on commit 8aaa664

Please sign in to comment.