500 server error when posting data containing non-existent fields

Support MB REST API 500 server error when posting data containing non-existent fieldsResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #42823
    danieldkdanieldk
    Participant

    I have a custom post type that I want to create via the REST API.

    When I do a POST to /wp-json/wp/v2/tmd_event with the following data:

    json
    {
        "post_title": "Testing non-existing meta_box value",
        "post_status": "draft",
        "post_type": "tmd_event",
        "meta_box": {
            "test": "value" ## note: this is not a custom field that exists!
        }
    }
    

    I get the following error:

    json
    {
        "code": "internal_server_error",
        "message": "<p>There has been a critical error on this website.</p><p><a href=\"https://wordpress.org/documentation/article/faq-troubleshooting/\">Learn more about troubleshooting WordPress.</a></p>",
        "data": {
            "status": 500
        },
        "additional_errors": []
    }
    

    If I leave out the field, the post gets created as a draft.

    I have looked into the WordPress debug.log, and found

    
    [06-Aug-2023 17:47:23 UTC] PHP Fatal error:  Uncaught TypeError: RWMB_Field::process_value(): Argument #3 ($field) must be of type array, bool given, called in /var/www/html/wp-content/plugins/meta-box-aio/vendor/meta-box/mb-rest-api/class-mb-rest-api.php on line 219 and defined in /var/www/html/wp-content/plugins/meta-box/inc/field.php:206
    Stack trace:
    #0 /var/www/html/wp-content/plugins/meta-box-aio/vendor/meta-box/mb-rest-api/class-mb-rest-api.php(219): RWMB_Field::process_value('valuze', 48164, false)
    #1 /var/www/html/wp-content/plugins/meta-box-aio/vendor/meta-box/mb-rest-api/class-mb-rest-api.php(84): MB_Rest_API->update_value(false, 'valuze', 48164)
    #2 /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(498): MB_Rest_API->update_post_meta(Array, Object(WP_Post), 'meta_box', Object(WP_REST_Request), 'tmd_event')
    #3 /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(747): WP_REST_Controller->update_additional_fields_for_object(Object(WP_Post), Object(WP_REST_Request))
    #4 /var/www/html/wp-includes/rest-api/class-wp-rest-server.php(1181): WP_REST_Posts_Controller->create_item(Object(WP_REST_Request))
    #5 /var/www/html/wp-includes/rest-api/class-wp-rest-server.php(1028): WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/wp/v2/tmd_even...', Array, NULL)
    #6 /var/www/html/wp-includes/rest-api/class-wp-rest-server.php(442): WP_REST_Server->dispatch(Object(WP_REST_Request))
    #7 /var/www/html/wp-includes/rest-api.php(410): WP_REST_Server->serve_request('/wp/v2/tmd_even...')
    #8 /var/www/html/wp-includes/class-wp-hook.php(308): rest_api_loaded(Object(WP))
    #9 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
    #10 /var/www/html/wp-includes/plugin.php(565): WP_Hook->do_action(Array)
    #11 /var/www/html/wp-includes/class-wp.php(399): do_action_ref_array('parse_request', Array)
    #12 /var/www/html/wp-includes/class-wp.php(780): WP->parse_request('')
    #13 /var/www/html/wp-includes/functions.php(1334): WP->main('')
    #14 /var/www/html/wp-blog-header.php(16): wp()
    #15 /var/www/html/index.php(17): require('/var/www/html/w...')
    #16 {main}
      thrown in /var/www/html/wp-content/plugins/meta-box/inc/field.php on line 206
    
    #42824
    danieldkdanieldk
    Participant

    My WordPress setup is:

    WordPress Version: 6.2.2
    Current WordPress Theme: Bootstrap4 Genesis version 1.3.0genesis
    Theme Author: Sal Ferrarello - https://salferrarello.com
    Theme URI: https://github.com/salcode/bootstrap4-genesis
    PHP Version: 8.0.28
    MySQL Version: 10.6.5-MariaDB-1:10.6.5+maria~focal
    Apache Version: Apache/2.4.56 (Debian)

    Meta Box (5.7.4)
    Meta Box AIO (1.21.4)
    REST API Toolbox (1.4.3)

    #42828
    PeterPeter
    Moderator

    Hello,

    MB Rest API only works with the custom fields created by Meta Box. You should update the field test only. Follow the documentation https://docs.metabox.io/extensions/mb-rest-api/

    #42834
    danieldkdanieldk
    Participant

    Dear Peter,

    thanks for your reply. From reading the documentation I see that the Metabox REST API extension only takes care of its own fields. Which makes perfect sense.

    However, what does not make sense is that it crashes when someone sends in a field that doesn't exist as a metabox field, or even doesn't exist as a custom field at all -- see my example.

    I propose that there is better error handling in the REST controller from the Metabox Extension. I see two behaviors:

    1. Ignore the field that doesn't exist, but don't crash.
    2. Ignore the field and return an error message that says "syntax error. field XXX doesnt exist".

    #42842
    PeterPeter
    Moderator

    Hello,

    Thanks for your feedback.

    I will inform the development team to consider improving this case in future updates.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.