Title: rest_convert_error_to_response
Published: March 9, 2021
Last modified: May 20, 2026

---

# rest_convert_error_to_response( WP_Error $error ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#wp--skip-link--target)

Converts an error to a response object.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#description)󠁿

This iterates over all error codes and messages to change 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](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#parameters)󠁿

 `$error`[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
required

[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) instance.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
List of associative arrays with code and message keys.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#source)󠁿

    ```php
    function rest_convert_error_to_response( $error ) {
    	$status = array_reduce(
    		$error->get_all_error_data(),
    		/**
    		 * @param int   $status     Status.
    		 * @param mixed $error_data Error data.
    		 */
    		static function ( int $status, $error_data ): int {
    			if ( is_array( $error_data ) && isset( $error_data['status'] ) && is_numeric( $error_data['status'] ) ) {
    				$status = (int) $error_data['status'];
    			}
    			return $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 WP_REST_Response( $data, $status );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/rest-api.php#L3420)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/rest-api.php#L3420-L3465)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_REST_Request::sanitize_params()](https://developer.wordpress.org/reference/classes/wp_rest_request/sanitize_params/)`wp-includes/rest-api/class-wp-rest-request.php` |

Sanitizes (where possible) the params on the request.

  | 
| [WP_REST_Request::has_valid_params()](https://developer.wordpress.org/reference/classes/wp_rest_request/has_valid_params/)`wp-includes/rest-api/class-wp-rest-request.php` |

Checks whether this request is valid according to its attributes.

  | 
| [WP_REST_Server::error_to_response()](https://developer.wordpress.org/reference/classes/wp_rest_server/error_to_response/)`wp-includes/rest-api/class-wp-rest-server.php` |

Converts an error to a response object.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/rest_convert_error_to_response/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.7.0](https://developer.wordpress.org/reference/since/5.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Frest_convert_error_to_response%2F)
before being able to contribute a note or feedback.