Title: WP_REST_Server::json_error
Published: December 9, 2015
Last modified: August 20, 2026

---

# WP_REST_Server::json_error( string $code, string $message, int|null $status = null ): string

## In this article

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

Retrieves an appropriate error representation in JSON.

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

Note: This should only be used in [WP_REST_Server::serve_request()](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_request/),
as it cannot handle [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
internally. All callbacks and other internal methods should instead return a [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
with the data set to an array that includes a ‘status’ key, with the value being
the HTTP status to send.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/json_error/?output_format=md#parameters)󠁿

 `$code`stringrequired

[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)-style code.

`$message`stringrequired

Human-readable message.

`$status`int|nulloptional

HTTP status code to send.

Default:`null`

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

 string JSON representation of the error.

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

    ```php
    protected function json_error( $code, $message, $status = null ) {
    	if ( $status ) {
    		$this->set_status( $status );
    	}

    	$error = compact( 'code', 'message' );

    	return wp_json_encode( $error );
    }
    ```

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

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

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

Sends an HTTP status code.

  | 
| [wp_json_encode()](https://developer.wordpress.org/reference/functions/wp_json_encode/)`wp-includes/functions.php` |

Encodes a variable into JSON, with some confidence checks.

  |

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

Handles serving a REST API request.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/json_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_server%2Fjson_error%2F)
before being able to contribute a note or feedback.