rest_convert_error_to_response example


                // If there's still no sanitize_callback, nothing to do here.                 if ( empty( $param_args['sanitize_callback'] ) ) {
                    continue;
                }

                /** @var mixed|WP_Error $sanitized_value */
                $sanitized_value = call_user_func( $param_args['sanitize_callback']$value$this$key );

                if ( is_wp_error( $sanitized_value ) ) {
                    $invalid_params[ $key ]  = implode( ' ', $sanitized_value->get_error_messages() );
                    $invalid_details[ $key ] = rest_convert_error_to_response( $sanitized_value )->get_data();
                } else {
                    $this->params[ $type ][ $key ] = $sanitized_value;
                }
            }
        }

        if ( $invalid_params ) {
            return new WP_Error(
                'rest_invalid_param',
                /* translators: %s: List of invalid parameters. */
                sprintf( __( 'Invalid parameter(s): %s' )implode( ', ', array_keys( $invalid_params ) ) ),
                

    protected function error_to_response( $error ) {
        return rest_convert_error_to_response( $error );
    }

    /** * Retrieves an appropriate error representation in JSON. * * Note: This should only be used in WP_REST_Server::serve_request(), as it * cannot handle WP_Error internally. All callbacks and other internal methods * should instead return a WP_Error with the data set to an array that includes * a 'status' key, with the value being the HTTP status to send. * * @since 4.4.0 * * @param string $code WP_Error-style code. * @param string $message Human-readable message. * @param int $status Optional. HTTP status code to send. Default null. * @return string JSON representation of the error */
Home | Imprint | This part of the site doesn't use cookies.