Skip to content

Commit 6f80ba2

Browse files
committed
Add error handling for edit/ajax requests where a required parameter is missing
If we're performing an edit action via ajax, and one of the required parameters is missing, we can provide a WP Error object with a message that contains the missing parameter. We then return the error message to the front end via die(), which kills the ajax request as well and allows for another attempt.
1 parent 8ca1610 commit 6f80ba2

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

ad-code-manager.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ function handle_admin_action() {
236236
else
237237
$id = $this->edit_ad_code( $id, $ad_code_vals );
238238
if ( is_wp_error( $id ) ) {
239-
$message = 'error-adding-editing-ad-code';
239+
// We can die with an error if this is an edit/ajax request
240+
if ( isset( $id->errors['edit-error'][0] ) )
241+
die( '<div class="error">' . $id->errors['edit-error'][0] . '</div>' );
242+
else
243+
$message = 'error-adding-editing-ad-code';
240244
break;
241245
}
242246
$new_conditionals = array();
@@ -433,10 +437,9 @@ function create_ad_code( $ad_code = array() ) {
433437
*/
434438
function edit_ad_code( $ad_code_id, $ad_code = array()) {
435439
foreach ( $this->current_provider->ad_code_args as $arg ) {
436-
// We shouldn't update an ad code,
437-
// If any of required fields is not set
440+
// If a required argument is not set, we return an error message with the missing parameter
438441
if ( ! $ad_code[$arg['key']] && $arg['required'] === true ) {
439-
return new WP_Error();
442+
return new WP_Error( 'edit-error', 'Error updating ad code, a parameter for ' . esc_html( $arg['key'] ) . ' is required.' );
440443
}
441444
}
442445
if ( 0 !== $ad_code_id ) {

0 commit comments

Comments
 (0)