Title: WP_REST_Response::as_error
Published: December 9, 2015
Last modified: February 24, 2026

---

# WP_REST_Response::as_error(): 󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿|null

## In this article

 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_response/as_error/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_response/as_error/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_response/as_error/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_response/as_error/?output_format=md#changelog)

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

Retrieves a [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object from the response.

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

 [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)|null [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
or null on not an errored response.

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

    ```php
    public function as_error() {
    	if ( ! $this->is_error() ) {
    		return null;
    	}

    	$error = new WP_Error();

    	if ( is_array( $this->get_data() ) ) {
    		$data = $this->get_data();
    		$error->add( $data['code'], $data['message'], $data['data'] );

    		if ( ! empty( $data['additional_errors'] ) ) {
    			foreach ( $data['additional_errors'] as $err ) {
    				$error->add( $err['code'], $err['message'], $err['data'] );
    			}
    		}
    	} else {
    		$error->add( $this->get_status(), '', array( 'status' => $this->get_status() ) );
    	}

    	return $error;
    }
    ```

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

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

| Uses | Description | 
| [WP_REST_Response::is_error()](https://developer.wordpress.org/reference/classes/wp_rest_response/is_error/)`wp-includes/rest-api/class-wp-rest-response.php` |

Checks if the response is an error, i.e. >= 400 response code.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

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

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

## User Contributed Notes

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