diff --git a/projects/plugins/jetpack/changelog/update-jetpack-ai-handle-errors-on-title-improvement-tool b/projects/plugins/jetpack/changelog/update-jetpack-ai-handle-errors-on-title-improvement-tool new file mode 100644 index 0000000000000..fa385b0412fcc --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-jetpack-ai-handle-errors-on-title-improvement-tool @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Title Optimization: properly handle errors and show the correct UI for each. diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/title-optimization/index.tsx b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/title-optimization/index.tsx index c272caf73a022..01acfac40d607 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/title-optimization/index.tsx +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/title-optimization/index.tsx @@ -1,15 +1,23 @@ /** * External dependencies */ -import { useAiSuggestions } from '@automattic/jetpack-ai-client'; +import { + useAiSuggestions, + RequestingErrorProps, + ERROR_QUOTA_EXCEEDED, + ERROR_NETWORK, + ERROR_SERVICE_UNAVAILABLE, + ERROR_UNCLEAR_PROMPT, +} from '@automattic/jetpack-ai-client'; import { useAnalytics } from '@automattic/jetpack-shared-extension-utils'; -import { Button, Spinner, ExternalLink } from '@wordpress/components'; +import { Button, Spinner, ExternalLink, Notice } from '@wordpress/components'; import { useDispatch } from '@wordpress/data'; import { useState, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ +import QuotaExceededMessage from '../../../../blocks/ai-assistant/components/quota-exceeded-message'; import { getFeatureAvailability } from '../../../../blocks/ai-assistant/lib/utils/get-feature-availability'; import useAutoSaveAndRedirect from '../../../../shared/use-autosave-and-redirect'; import usePostContent from '../../hooks/use-post-content'; @@ -25,6 +33,43 @@ const isKeywordsFeatureAvailable = getFeatureAvailability( 'ai-title-optimization-keywords-support' ); +/** + * A generic error message that we can reuse. + */ +const genericErrorMessage = __( + 'The generation of your suggested titles failed. Please try again.', + 'jetpack' +); + +const ERROR_JSON_PARSE = 'json-parse-error'; +type TitleOptimizationJSONError = { + code: typeof ERROR_JSON_PARSE; + message: string; +}; + +type TitleOptimizationError = RequestingErrorProps | TitleOptimizationJSONError; + +const TitleOptimizationErrorMessage = ( { error }: { error: TitleOptimizationError } ) => { + if ( error.code === ERROR_QUOTA_EXCEEDED ) { + return ( +
{ sidebarDescription }
@@ -190,12 +247,7 @@ export default function TitleOptimization( { ) : ( <> { error ? ( -