rest_convert_error_to_response()
最后更新于:2021-11-27 23:33:42
rest_convert_error_to_response( GC_Error$error)Converts an error to a response object.
参数
响应
(GC_REST_Response) List of associative arrays with code and message keys.
源文件
文件: gc-includes/rest-api.php
function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->get_all_error_data(),
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 => $messages ) {
$all_data = $error->get_all_error_data( $code );
$last_data = array_pop( $all_data );
foreach ( (array) $messages as $message ) {
$formatted = array(
'code' => $code,
'message' => $message,
'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 GC_REST_Response( $data, $status );
}