WP Kernel API / core/src / ServerError
Class: ServerError
Error thrown when WordPress REST API returns an error
Example
throw new ServerError({
serverCode: 'rest_forbidden',
serverMessage: 'Sorry, you are not allowed to do that.',
status: 403,
path: '/wpk/v1/things',
method: 'POST'
});
Extends
Constructors
Constructor
new ServerError(options): ServerError;
Create a new ServerError
Parameters
options
Server error options
serverCode
string
serverMessage
string
status
number
path
string
method
string
serverData?
Record
<string
, unknown
>
context?
Returns
ServerError
Overrides
Properties
code
readonly code: ErrorCode;
Error code - identifies the type of error
Inherited from
data?
readonly optional data: ErrorData;
Additional data about the error
Inherited from
context?
readonly optional context: ErrorContext;
Context in which the error occurred
Inherited from
serverCode
readonly serverCode: string;
WordPress error code (e.g., 'rest_forbidden', 'rest_invalid_param')
serverMessage
readonly serverMessage: string;
WordPress error message
status
readonly status: number;
HTTP status code
path
readonly path: string;
Request path
method
readonly method: string;
HTTP method
serverData?
readonly optional serverData: Record<string, unknown>;
Additional server data
Methods
toJSON()
toJSON(): SerializedError;
Serialize error to JSON-safe format
Returns
Serialized error object
Inherited from
fromJSON()
static fromJSON(serialized): KernelError;
Create KernelError from serialized format
Parameters
serialized
Serialized error object
Returns
New KernelError instance
Inherited from
isKernelError()
static isKernelError(error): error is KernelError;
Check if an error is a KernelError
Parameters
error
unknown
Error to check
Returns
error is KernelError
True if error is a KernelError
Inherited from
wrap()
static wrap(
error,
code,
context?): KernelError;
Wrap a native Error into a KernelError
Parameters
error
Error
Native error to wrap
code
ErrorCode
= 'UnknownError'
Error code to assign
context?
Additional context
Returns
New KernelError wrapping the original
Inherited from
fromWordPressResponse()
static fromWordPressResponse(
response,
path,
method,
context?): ServerError;
Parse WordPress REST API error response into ServerError
Parameters
response
WordPress REST error response
path
string
Request path
method
string
HTTP method
context?
Additional context
Returns
ServerError
New ServerError instance
isPermissionError()
isPermissionError(): boolean;
Check if this is a permission/capability error
Returns
boolean
True if this is a permission error
isValidationError()
isValidationError(): boolean;
Check if this is a validation error
Returns
boolean
True if this is a validation error
isNotFoundError()
isNotFoundError(): boolean;
Check if this is a "not found" error
Returns
boolean
True if resource was not found
getValidationErrors()
getValidationErrors(): object[];
Extract validation errors from server response
Returns
object
[]
Array of validation errors if available