Automattic\WooCommerce\Internal\RestApi\Routes\V4

AbstractController::get_route_error_by_codeprotectedWC 1.0

Get an error response for a given error code.

Method of the class: AbstractController{}

No Hooks.

Returns

WP_Error. WP Error object.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_route_error_by_code( $error_code ): WP_Error;
$error_code(string) (required)
The error code.

AbstractController::get_route_error_by_code() code WC 10.4.3

protected function get_route_error_by_code( string $error_code ): WP_Error {
	$error_messages    = array(
		self::INVALID_ID          => __( 'Invalid ID.', 'woocommerce' ),
		self::RESOURCE_EXISTS     => __( 'Resource already exists.', 'woocommerce' ),
		self::CANNOT_CREATE       => __( 'Cannot create resource.', 'woocommerce' ),
		self::CANNOT_DELETE       => __( 'Cannot delete resource.', 'woocommerce' ),
		self::CANNOT_UPDATE       => __( 'Cannot update resource.', 'woocommerce' ),
		self::CANNOT_TRASH        => __( 'Cannot trash resource.', 'woocommerce' ),
		self::TRASH_NOT_SUPPORTED => __( 'Trash not supported.', 'woocommerce' ),
	);
	$http_status_codes = array(
		self::INVALID_ID          => WP_Http::NOT_FOUND,
		self::RESOURCE_EXISTS     => WP_Http::BAD_REQUEST,
		self::CANNOT_CREATE       => WP_Http::INTERNAL_SERVER_ERROR,
		self::CANNOT_DELETE       => WP_Http::INTERNAL_SERVER_ERROR,
		self::CANNOT_UPDATE       => WP_Http::INTERNAL_SERVER_ERROR,
		self::CANNOT_TRASH        => WP_Http::GONE,
		self::TRASH_NOT_SUPPORTED => WP_Http::NOT_IMPLEMENTED,
	);
	return $this->get_route_error_response(
		$this->get_error_prefix() . $error_code,
		$error_messages[ $error_code ] ?? __( 'An error occurred while processing your request.', 'woocommerce' ),
		$http_status_codes[ $error_code ] ?? WP_Http::BAD_REQUEST
	);
}