Make WordPress Core


Ignore:
Timestamp:
02/13/2024 09:55:38 AM (3 years ago)
Author:
spacedmonkey
Message:

REST API: Improve error handling in REST meta fields

This update modifies the error handling mechanism in the REST API meta fields functionality. Instead of halting execution and returning on the first encountered error, it now collects all errors in a WP_Error object and continues execution. Thus, this enhancement enables handling and displaying of multiple errors in a single response, improving the debugging process.

Props TimothyBlynJacobs, spacedmonkey, hellofromTonya, oglekler.
Fixes #48823.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r56075 r57611  
    142142        public function update_value( $meta, $object_id ) {
    143143                $fields = $this->get_registered_fields();
     144                $error  = new WP_Error();
    144145
    145146                foreach ( $fields as $meta_key => $args ) {
     
    164165
    165166                                        if ( is_wp_error( rest_validate_value_from_schema( $current, $args['schema'] ) ) ) {
    166                                                 return new WP_Error(
     167                                                $error->add(
    167168                                                        'rest_invalid_stored_value',
    168169                                                        /* translators: %s: Custom field key. */
     
    170171                                                        array( 'status' => 500 )
    171172                                                );
     173                                                continue;
    172174                                        }
    173175                                }
     
    175177                                $result = $this->delete_meta_value( $object_id, $meta_key, $name );
    176178                                if ( is_wp_error( $result ) ) {
    177                                         return $result;
     179                                        $error->merge_from( $result );
    178180                                }
    179181                                continue;
     
    181183
    182184                        if ( ! $args['single'] && is_array( $value ) && count( array_filter( $value, 'is_null' ) ) ) {
    183                                 return new WP_Error(
     185                                $error->add(
    184186                                        'rest_invalid_stored_value',
    185187                                        /* translators: %s: Custom field key. */
     
    187189                                        array( 'status' => 500 )
    188190                                );
     191                                continue;
    189192                        }
    190193
     
    192195                        if ( is_wp_error( $is_valid ) ) {
    193196                                $is_valid->add_data( array( 'status' => 400 ) );
    194                                 return $is_valid;
     197                                $error->merge_from( $is_valid );
     198                                continue;
    195199                        }
    196200
     
    204208
    205209                        if ( is_wp_error( $result ) ) {
    206                                 return $result;
    207                         }
     210                                $error->merge_from( $result );
     211                                continue;
     212                        }
     213                }
     214
     215                if ( $error->has_errors() ) {
     216                        return $error;
    208217                }
    209218
Note: See TracChangeset for help on using the changeset viewer.