@wpkernel/core / 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
method
string
path
string
serverCode
string
serverMessage
string
status
number
context?
serverData?
Record<string, unknown>
Returns
ServerError
Overrides
Properties
code
readonly code: ErrorCode;Error code - identifies the type of error
Inherited from
method
readonly method: string;HTTP method
path
readonly path: string;Request path
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
context?
readonly optional context: ErrorContext;Context in which the error occurred
Inherited from
data?
readonly optional data: ErrorData;Additional data about the error
Inherited from
serverData?
readonly optional serverData: Record<string, unknown>;Additional server data
Methods
fromJSON()
static fromJSON(serialized): WPKernelError;Create WPKernelError from serialized format
Parameters
serialized
Serialized error object
Returns
New WPKernelError instance
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
getValidationErrors()
getValidationErrors(): object[];Extract validation errors from server response
Returns
object[]
Array of validation errors if available
isNotFoundError()
isNotFoundError(): boolean;Check if this is a "not found" error
Returns
boolean
True if resource was not found
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
isWPKernelError()
static isWPKernelError(error): error is WPKernelError;Check if an error is a WPKernelError
Parameters
error
unknown
Error to check
Returns
error is WPKernelError
True if error is a WPKernelError
Inherited from
toJSON()
toJSON(): SerializedError;Serialize error to JSON-safe format
Returns
Serialized error object
Inherited from
wrap()
static wrap(
error,
code,
context?): WPKernelError;Wrap a native Error into a WPKernelError
Parameters
error
Error
Native error to wrap
code
ErrorCode = 'UnknownError'
Error code to assign
context?
Additional context
Returns
New WPKernelError wrapping the original
