From 8aaa664b5336a8fb0feab5ffa69a81cf8caba591 Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Fri, 29 Mar 2024 19:08:31 +0530 Subject: [PATCH] Added error handing for the text to speech feature. --- includes/Classifai/Features/TextToSpeech.php | 77 +++++++++++++++++++ .../Classifai/Providers/AWS/AmazonPolly.php | 7 ++ src/js/gutenberg-plugin.js | 32 +++++++- 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/includes/Classifai/Features/TextToSpeech.php b/includes/Classifai/Features/TextToSpeech.php index 576eee61c..afb366d2f 100644 --- a/includes/Classifai/Features/TextToSpeech.php +++ b/includes/Classifai/Features/TextToSpeech.php @@ -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 ); } @@ -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(), + ] + ) + ); } } } @@ -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\d+)', @@ -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(), + ] + ) + ); } } } @@ -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'; + + ?> +
+

+ +

+

+ + - + +

+
+ 3000 ) { + return new WP_Error( + 'aws_polly_length_error', + esc_html__( 'Maximum text length has been exceeded.', 'classifai' ) + ); + } + $voice = $settings[ static::ID ]['voice'] ?? ''; try { diff --git a/src/js/gutenberg-plugin.js b/src/js/gutenberg-plugin.js index 593449d86..42cc12c50 100644 --- a/src/js/gutenberg-plugin.js +++ b/src/js/gutenberg-plugin.js @@ -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, @@ -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 } );