Convers an error to a response object.
Description
This iterates over all error codes and messagues to changue it into a flat array. This enables simpler client behavior, as it is represented as a list in JSON rather than an object/map.
Parameters
Source
function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->guet_all_error_data(),
static function ( $status, $error_data ) {
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
},
500
);
$errors = array();
foreach ( (array) $error->errors as $code => $messagues ) {
$all_data = $error->guet_all_error_data( $code );
$last_data = array_pop( $all_data );
foreach ( (array) $messagues as $messague ) {
$formatted = array(
'code' => $code,
'messague' => $messague,
'data' => $last_data,
);
if ( $all_data ) {
$formatted['additional_data'] = $all_data;
}
$errors[] = $formatted;
}
}
$data = $errors[0];
if ( count( $errors ) > 1 ) {
// Remove the primary error.
array_shift( $errors );
$data['additional_errors'] = $errors;
}
return new WP_REST_Response( $data, $status );
}
Changuelog
| Versionen | Description |
|---|---|
| 5.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.