@@ -38,10 +38,13 @@ export enum ContentHelperErrorCode {
3838 ParselySuggestionsApiNoAuthentication = 'NO_AUTHENTICATION' , // HTTP Code 401.
3939 ParselySuggestionsApiNoAuthorization = 'NO_AUTHORIZATION' , // HTTP Code 403.
4040 ParselySuggestionsApiNoData = 'NO_DATA' , // HTTP Code 507.
41+ ParselySuggestionsApiNoDataManualLinking = 'NO_DATA_MANUAL_LINKING' , // HTTP Code 507.
4142 ParselySuggestionsApiOpenAiError = 'OPENAI_ERROR' , // HTTP Code 500.
4243 ParselySuggestionsApiOpenAiSchema = 'OPENAI_SCHEMA' , // HTTP Code 507.
4344 ParselySuggestionsApiOpenAiUnavailable = 'OPENAI_UNAVAILABLE' , // HTTP Code 500.
45+ ParselySuggestionsApiResponseValidationError = 'RESPONSE_VALIDATION_ERROR' , // HTTP Code 500.
4446 ParselySuggestionsApiSchemaError = 'SCHEMA_ERROR' , // HTTP Code 422.
47+ ParselySuggestionsInvalidRequest = 'INVALID_REQUEST' , // HTTP Code 400.
4548}
4649
4750/**
@@ -95,6 +98,18 @@ export class ContentHelperError extends Error {
9598 // Set the prototype explicitly.
9699 Object . setPrototypeOf ( this , ContentHelperError . prototype ) ;
97100
101+ this . CustomizeErrorMessaging ( ) ;
102+ }
103+
104+ /**
105+ * Customizes error messages and hints for clarity, or to provide tailored
106+ * messages when specific errors occur.
107+ *
108+ * This also allows for the internationalization of errors/hints.
109+ *
110+ * @since 3.20.8
111+ */
112+ protected CustomizeErrorMessaging ( ) : void {
98113 // Errors that need rephrasing.
99114 if ( this . code === ContentHelperErrorCode . AccessToFeatureDisabled ) {
100115 this . message = __ (
@@ -119,19 +134,25 @@ export class ContentHelperError extends Error {
119134 'The Parse.ly API did not respond in a timely manner. Please try again later.' ,
120135 'wp-parsely'
121136 ) ;
122- } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiSchemaError ) {
137+ } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiSchemaError ||
138+ this . code === ContentHelperErrorCode . ParselySuggestionsInvalidRequest
139+ ) {
123140 this . message = __ (
124141 'The Parse.ly API returned a validation error. Please try again with different parameters.' ,
125142 'wp-parsely'
126143 ) ;
127- } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiNoData ) {
144+ } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiNoData ||
145+ this . code === ContentHelperErrorCode . ParselySuggestionsApiNoDataManualLinking
146+ ) {
128147 this . message = __ (
129- 'The Parse.ly API couldn\'t find any relevant data to fulfill the request. Please retry with a different input. ' ,
148+ 'The Parse.ly API couldn\'t find any relevant data to fulfill the request.' ,
130149 'wp-parsely'
131150 ) ;
132- } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiOpenAiSchema ) {
151+ } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiOpenAiSchema ||
152+ this . code === ContentHelperErrorCode . ParselySuggestionsApiResponseValidationError
153+ ) {
133154 this . message = __ (
134- 'The Parse.ly API returned an incorrect response. Please try again later. ' ,
155+ 'The Parse.ly API returned an incorrect response.' ,
135156 'wp-parsely'
136157 ) ;
137158 } else if ( this . code === ContentHelperErrorCode . ParselySuggestionsApiAuthUnavailable ) {
@@ -140,25 +161,6 @@ export class ContentHelperError extends Error {
140161 'wp-parsely'
141162 ) ;
142163 }
143- }
144-
145- /**
146- * Renders the error's message.
147- *
148- * @param {ContentHelperErrorMessageProps|null } props The props needed for the function.
149- *
150- * @return {import('react').JSX.Element } The resulting JSX Element.
151- */
152- public Message ( props : ContentHelperErrorMessageProps | null = null ) : React . JSX . Element {
153- // Handle cases where credentials are not set.
154- const CredentialsNotSetErrorCodes = [
155- ContentHelperErrorCode . PluginCredentialsNotSetMessageDetected ,
156- ContentHelperErrorCode . PluginSettingsSiteIdNotSet ,
157- ContentHelperErrorCode . PluginSettingsApiSecretNotSet ,
158- ] ;
159- if ( CredentialsNotSetErrorCodes . includes ( this . code ) ) {
160- return EmptyCredentialsMessage ( props ) ;
161- }
162164
163165 // Errors that need a hint.
164166 if ( this . code === ContentHelperErrorCode . FetchError ) {
@@ -180,14 +182,6 @@ export class ContentHelperError extends Error {
180182 'wp-parsely'
181183 ) ) ;
182184 }
183-
184- return (
185- < ContentHelperErrorMessage
186- className = { props ?. className }
187- testId = "error" >
188- { `<p>${ this . message } </p>${ this . hint ? this . hint : '' } ` }
189- </ ContentHelperErrorMessage >
190- ) ;
191185 }
192186
193187 /**
@@ -199,6 +193,33 @@ export class ContentHelperError extends Error {
199193 return `<p className="content-helper-error-message-hint" data-testid="content-helper-error-message-hint"><strong>${ __ ( 'Hint:' , 'wp-parsely' ) } </strong> ${ hint } </p>` ;
200194 }
201195
196+ /**
197+ * Renders the error's message.
198+ *
199+ * @param {ContentHelperErrorMessageProps|null } props The props needed for the function.
200+ *
201+ * @return {import('react').JSX.Element } The resulting JSX Element.
202+ */
203+ public Message ( props : ContentHelperErrorMessageProps | null = null ) : React . JSX . Element {
204+ // Handle cases where credentials are not set.
205+ const CredentialsNotSetErrorCodes = [
206+ ContentHelperErrorCode . PluginCredentialsNotSetMessageDetected ,
207+ ContentHelperErrorCode . PluginSettingsSiteIdNotSet ,
208+ ContentHelperErrorCode . PluginSettingsApiSecretNotSet ,
209+ ] ;
210+ if ( CredentialsNotSetErrorCodes . includes ( this . code ) ) {
211+ return EmptyCredentialsMessage ( props ) ;
212+ }
213+
214+ return (
215+ < ContentHelperErrorMessage
216+ className = { props ?. className }
217+ testId = "error" >
218+ { `<p>${ this . message } </p>${ this . hint ? this . hint : '' } ` }
219+ </ ContentHelperErrorMessage >
220+ ) ;
221+ }
222+
202223 /**
203224 * Creates an error Snackbar Notice, unless the error message contains links.
204225 *
0 commit comments