Skip to content

Commit

Permalink
Protect: More Descriptive Error Messages (#29185)
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller authored Mar 7, 2023
1 parent fc9b6b5 commit dff2d71
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Minor improvement to error messages on the firewall settings screen.


32 changes: 24 additions & 8 deletions projects/plugins/protect/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static function api_clear_scan_cache() {
*/
public static function api_ignore_threat( $request ) {
if ( ! $request['threat_id'] ) {
return new WP_REST_RESPONSE( 'Missing threat ID.', 400 );
return new WP_REST_Response( 'Missing threat ID.', 400 );
}

$threat_ignored = Threats::ignore_threat( $request['threat_id'] );
Expand All @@ -275,7 +275,7 @@ public static function api_ignore_threat( $request ) {
*/
public static function api_fix_threats( $request ) {
if ( empty( $request['threat_ids'] ) ) {
return new WP_REST_RESPONSE( 'Missing threat IDs.', 400 );
return new WP_REST_Response( 'Missing threat IDs.', 400 );
}

$threats_fixed = Threats::fix_threats( $request['threat_ids'] );
Expand All @@ -296,7 +296,7 @@ public static function api_fix_threats( $request ) {
*/
public static function api_fix_threats_status( $request ) {
if ( empty( $request['threat_ids'] ) ) {
return new WP_REST_RESPONSE( 'Missing threat IDs.', 400 );
return new WP_REST_Response( 'Missing threat IDs.', 400 );
}

$threats_fixed = Threats::fix_threats_status( $request['threat_ids'] );
Expand Down Expand Up @@ -341,16 +341,32 @@ public static function api_scan() {
/**
* Toggles the WAF module on or off for the API endpoint
*
* @return WP_REST_Response
* @return WP_REST_Response|WP_Error
*/
public static function api_toggle_waf() {
if ( Waf_Runner::is_enabled() ) {
Waf_Runner::disable();
return rest_ensure_response( true, 200 );
$disabled = Waf_Runner::disable();
if ( ! $disabled ) {
return new WP_Error(
'waf_disable_failed',
__( 'An error occured disabling the firewall.', 'jetpack-protect' ),
array( 'status' => 500 )
);
}

return rest_ensure_response( true );
}

$enabled = Waf_Runner::enable();
if ( ! $enabled ) {
return new WP_Error(
'waf_enable_failed',
__( 'An error occured enabling the firewall.', 'jetpack-protect' ),
array( 'status' => 500 )
);
}

Waf_Runner::enable();
return rest_ensure_response( true, 200 );
return rest_ensure_response( true );
}

/**
Expand Down
93 changes: 62 additions & 31 deletions projects/plugins/protect/src/js/components/firewall-page/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ import styles from './styles.module.scss';
const ADMIN_URL = window?.jetpackProtectInitialState?.adminUrl;
const SUCCESS_NOTICE_DURATION = 5000;

const errorMessage = createInterpolateElement(
__(
'An error ocurred. Please try again or <supportLink>contact support</supportLink>.',
'jetpack-protect'
),
{
supportLink: <ExternalLink href={ PLUGIN_SUPPORT_URL } />,
}
);

const FirewallPage = () => {
const [ isSmall ] = useBreakpointMatch( [ 'sm', 'lg' ], [ null, '<' ] );
const notice = useSelect( select => select( STORE_ID ).getNotice() );
Expand Down Expand Up @@ -110,6 +100,52 @@ const FirewallPage = () => {
*/
const [ showManualRules, setShowManualRules ] = useState( false );

/**
* Get a custom error message based on the error code.
*
* @param {object} error - Error object.
* @returns string|bool Custom error message or false if no custom message exists.
*/
const getCustomErrorMessage = useCallback( error => {
switch ( error.code ) {
case 'file_system_error':
return __( 'A filesystem error occurred.', 'jetpack-protect' );
case 'rules_api_error':
return __(
'An error occurred retrieving the latest firewall rules from Jetpack.',
'jetpack-protect'
);
default:
return false;
}
}, [] );

/**
* Handle errors returned by the API.
*/
const handleApiError = useCallback(
error => {
const errorMessage =
getCustomErrorMessage( error ) || __( 'An error occurred.', 'jetpack-protect' );
const supportMessage = createInterpolateElement(
__( 'Please try again or <supportLink>contact support</supportLink>.', 'jetpack-protect' ),
{
supportLink: <ExternalLink href={ PLUGIN_SUPPORT_URL } />,
}
);

setNotice( {
type: 'error',
message: (
<>
{ errorMessage } { supportMessage }
</>
),
} );
},
[ getCustomErrorMessage, setNotice ]
);

/**
* Get Scan
*
Expand Down Expand Up @@ -137,14 +173,9 @@ const FirewallPage = () => {
message: __( 'Changes saved.', 'jetpack-protect' ),
} )
)
.catch( () => {
setNotice( {
type: 'error',
message: errorMessage,
} );
} )
.catch( handleApiError )
.finally( () => setFormIsSubmitting( false ) );
}, [ formState, updateConfig, setNotice ] );
}, [ updateConfig, formState, handleApiError, setNotice ] );

/**
* Handle Change
Expand Down Expand Up @@ -197,15 +228,19 @@ const FirewallPage = () => {
API.wafUpgradeSeen();
}
} )
.catch( () => {
.catch( error => {
setAutomaticRulesInstallationError( true );
setNotice( {
type: 'error',
message: errorMessage,
} );
handleApiError( error );
} )
.finally( () => setFormIsSubmitting( false ) );
}, [ formState, toggleAutomaticRules, setNotice, upgradeIsSeen, setWafUpgradeIsSeen ] );
}, [
formState,
toggleAutomaticRules,
setNotice,
upgradeIsSeen,
setWafUpgradeIsSeen,
handleApiError,
] );

/**
* Handle Manual Rules Change
Expand All @@ -232,14 +267,9 @@ const FirewallPage = () => {
),
} )
)
.catch( () => {
setNotice( {
type: 'error',
message: errorMessage,
} );
} )
.catch( handleApiError )
.finally( () => setFormIsSubmitting( false ) );
}, [ formState, toggleManualRules, setNotice ] );
}, [ formState, toggleManualRules, handleApiError, setNotice ] );

/**
* Handle Show Manual Rules Click
Expand Down Expand Up @@ -432,7 +462,8 @@ const FirewallPage = () => {
variant={ 'body-small' }
mt={ 2 }
>
{ __( 'Failed to install automatic rules.', 'jetpack-protect' ) }
{ __( 'Failed to update automatic rules.', 'jetpack-protect' ) }{ ' ' }
{ getCustomErrorMessage( automaticRulesInstallationError ) }
</Text>
<Button variant={ 'link' } href={ PLUGIN_SUPPORT_URL }>
<Text variant={ 'body-small' }>
Expand Down

0 comments on commit dff2d71

Please sign in to comment.