Skip to content

Commit 0f4fe99

Browse files
authored
Merge pull request #3701 from Parsely/update/include-more-errors-to-content-helper-error-tsx
2 parents 0001f7b + f3ada3e commit 0f4fe99

File tree

10 files changed

+71
-47
lines changed

10 files changed

+71
-47
lines changed

build/admin-settings.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-data', 'wp-escape-html', 'wp-i18n', 'wp-url'), 'version' => '66f2b3e882defbfd9d95');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-data', 'wp-escape-html', 'wp-i18n', 'wp-url'), 'version' => '2241911e2fdfed629bf6');

build/admin-settings.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-escape-html', 'wp-i18n', 'wp-notices', 'wp-primitives', 'wp-url'), 'version' => '620219ec63d477a4c918');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-escape-html', 'wp-i18n', 'wp-notices', 'wp-primitives', 'wp-url'), 'version' => '8acfeb018da81a942e25');

build/content-helper/dashboard-page.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '9dd4e7d94d11e6d6f594');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '880b3ef462175993e9b1');

build/content-helper/dashboard-widget.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => '3aca3610bec0d7b11473');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => '84075f31925e3bed158a');

build/content-helper/editor-sidebar.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content-helper/common/content-helper-error.tsx

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

tests/js/content-helper/content-helper-error.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ const errorCodes = [
2929
ContentHelperErrorCode.ParselySuggestionsApiNoAuthentication,
3030
ContentHelperErrorCode.ParselySuggestionsApiNoAuthorization,
3131
ContentHelperErrorCode.ParselySuggestionsApiNoData,
32+
ContentHelperErrorCode.ParselySuggestionsApiNoDataManualLinking,
3233
ContentHelperErrorCode.ParselySuggestionsApiOpenAiError,
3334
ContentHelperErrorCode.ParselySuggestionsApiOpenAiSchema,
3435
ContentHelperErrorCode.ParselySuggestionsApiOpenAiUnavailable,
36+
ContentHelperErrorCode.ParselySuggestionsApiResponseValidationError,
3537
ContentHelperErrorCode.ParselySuggestionsApiSchemaError,
38+
ContentHelperErrorCode.ParselySuggestionsInvalidRequest,
3639
];
3740

3841
/**

0 commit comments

Comments
 (0)