Automattic\WooCommerce\Internal

RestApiControllerBase::run()protectedWC 1.0

Handle a request for one of the provided REST API endpoints.

If an exception is thrown, the exception message will be returned as part of the response if the user has the 'manage_woocommerce' capability.

Note that the method specified in $method_name must have a 'protected' visibility and accept one argument of type 'WP_REST_Request'.

Method of the class: RestApiControllerBase{}

No Hooks.

Return

WP_Error|WP_HTTP_Response|WP_REST_Response. The response to send back to the client.

Usage

// protected - for code of main (parent) or child class
$result = $this->run( $request, $method_name );
$request(WP_REST_Request) (required)
The incoming HTTP REST request.
$method_name(string) (required)
The name of the class method to execute. It must be protected and accept one argument of type 'WP_REST_Request'.

RestApiControllerBase::run() code WC 9.3.3

protected function run( WP_REST_Request $request, string $method_name ) {
	try {
		return rest_ensure_response( $this->$method_name( $request ) );
	} catch ( InvalidArgumentException $ex ) {
		$message = $ex->getMessage();
		return new WP_Error( 'woocommerce_rest_invalid_argument', $message ? $message : __( 'Internal server error', 'woocommerce' ), array( 'status' => 400 ) );
	} catch ( Exception $ex ) {
		wc_get_logger()->error( StringUtil::class_name_without_namespace( static::class ) . ": when executing method $method_name: {$ex->getMessage()}" );
		return $this->internal_wp_error( $ex );
	}
}