RestResponse
in package
FinalYes
Standardized REST response envelope.
All responses always include: success, data, meta, message, errors.
7 canonical error codes: VALIDATION_ERROR (422), AUTHENTICATION_ERROR (401), AUTHORIZATION_ERROR (403), NOT_FOUND (404), CONFLICT (409), RATE_LIMIT (429), INTERNAL_ERROR (500)
Table of Contents
Constants
- ERR_AUTHENTICATION : string = 'AUTHENTICATION_ERROR'
- ERR_AUTHORIZATION : string = 'AUTHORIZATION_ERROR'
- ERR_CONFLICT : string = 'CONFLICT'
- ERR_INTERNAL : string = 'INTERNAL_ERROR'
- ERR_NOT_FOUND : string = 'NOT_FOUND'
- ERR_RATE_LIMIT : string = 'RATE_LIMIT'
- ERR_VALIDATION : string = 'VALIDATION_ERROR'
Methods
- conflict() : WP_REST_Response
- 409 — State conflict.
- created() : WP_REST_Response
- error() : WP_REST_Response
- Generic error. Prefer the semantic helpers below.
- forbidden() : WP_REST_Response
- 403 — Authenticated but no permission.
- internalError() : WP_REST_Response
- 500 — Unexpected internal error.
- noContent() : WP_REST_Response
- notFound() : WP_REST_Response
- 404 — Resource not found.
- paginated() : WP_REST_Response
- rateLimit() : WP_REST_Response
- 429 — Rate limit exceeded. Sets Retry-After header.
- success() : WP_REST_Response
- unauthorized() : WP_REST_Response
- 401 — Missing, invalid, or expired token.
- validationError() : WP_REST_Response
- 422 — Input validation failed.
Constants
ERR_AUTHENTICATION
public
string
ERR_AUTHENTICATION
= 'AUTHENTICATION_ERROR'
ERR_AUTHORIZATION
public
string
ERR_AUTHORIZATION
= 'AUTHORIZATION_ERROR'
ERR_CONFLICT
public
string
ERR_CONFLICT
= 'CONFLICT'
ERR_INTERNAL
public
string
ERR_INTERNAL
= 'INTERNAL_ERROR'
ERR_NOT_FOUND
public
string
ERR_NOT_FOUND
= 'NOT_FOUND'
ERR_RATE_LIMIT
public
string
ERR_RATE_LIMIT
= 'RATE_LIMIT'
ERR_VALIDATION
public
string
ERR_VALIDATION
= 'VALIDATION_ERROR'
Methods
conflict()
409 — State conflict.
public
static conflict(string $message[, string $detail = '' ]) : WP_REST_Response
Parameters
- $message : string
- $detail : string = ''
Return values
WP_REST_Responsecreated()
public
static created([mixed $data = null ][, string|null $message = null ]) : WP_REST_Response
Parameters
- $data : mixed = null
- $message : string|null = null
Return values
WP_REST_Responseerror()
Generic error. Prefer the semantic helpers below.
public
static error(string $code, string $message[, int $status = 400 ][, null|array<string, mixed> $errors = null ]) : WP_REST_Response
Parameters
- $code : string
- $message : string
- $status : int = 400
- $errors : null|array<string, mixed> = null
-
Additional error context
Return values
WP_REST_Responseforbidden()
403 — Authenticated but no permission.
public
static forbidden([string $message = 'Access denied.' ][, string $detail = '' ]) : WP_REST_Response
Parameters
- $message : string = 'Access denied.'
- $detail : string = ''
Return values
WP_REST_ResponseinternalError()
500 — Unexpected internal error.
public
static internalError([string $message = 'Internal server error.' ]) : WP_REST_Response
Parameters
- $message : string = 'Internal server error.'
Return values
WP_REST_ResponsenoContent()
public
static noContent() : WP_REST_Response
Return values
WP_REST_ResponsenotFound()
404 — Resource not found.
public
static notFound([string $message = 'Resource not found.' ]) : WP_REST_Response
Parameters
- $message : string = 'Resource not found.'
Return values
WP_REST_Responsepaginated()
public
static paginated(array<string|int, mixed> $data, int $total, int $perPage, int $currentPage) : WP_REST_Response
Parameters
- $data : array<string|int, mixed>
- $total : int
- $perPage : int
- $currentPage : int
Return values
WP_REST_ResponserateLimit()
429 — Rate limit exceeded. Sets Retry-After header.
public
static rateLimit([string $message = 'Rate limit exceeded.' ][, int $retryAfter = 60 ]) : WP_REST_Response
Parameters
- $message : string = 'Rate limit exceeded.'
- $retryAfter : int = 60
Return values
WP_REST_Responsesuccess()
public
static success([mixed $data = null ][, int $status = 200 ][, string|null $message = null ]) : WP_REST_Response
Parameters
- $data : mixed = null
- $status : int = 200
- $message : string|null = null
Return values
WP_REST_Responseunauthorized()
401 — Missing, invalid, or expired token.
public
static unauthorized([string $message = 'Unauthorized.' ][, string $detail = '' ]) : WP_REST_Response
Parameters
- $message : string = 'Unauthorized.'
- $detail : string = ''
Return values
WP_REST_ResponsevalidationError()
422 — Input validation failed.
public
static validationError(array<string|int, mixed> $fields[, string $message = 'Validation failed.' ]) : WP_REST_Response
Parameters
- $fields : array<string|int, mixed>
- $message : string = 'Validation failed.'