Uncaught Error: Class ‘Api\WP_Error’ not found in wordpress

Solution:

You need set return new \WP_Error It’s should help you. See here