• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Resolved How to handle API fatal error?

nethubonline

Regular Pleskian
Hi all,

According to plesk/api-php-lib , I tried example codes as below:

Code:
<?php

require 'vendor/autoload.php';

$host = 'testing.com';
$login = 'admin';
$password = '123456';

$client = new \PleskX\Api\Client($host);
$client->setCredentials($login, $password);

$client->customer()->create([
    'cname' => 'Plesk',
    'pname' => 'John Smith',
    'login' => 'john',
    'passwd' => 'secret',
    'email' => '[email protected]',
]);
?>

I ran the PHP and it can create customer successfully, and then I ran it again, error was shown below. As you see, above PHP code does not echo any error, and it somehow display the error automatically. May I know how I can handle the error and display it in my own way? Thanks a lot.

Fatal error: Uncaught PleskX\Api\Exception: User account already exists. in /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Client.php:282 Stack trace: #0 /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Client.php(161): PleskX\Api\Client->_verifyResponse(Object(PleskX\Api\XmlResponse)) #1 /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Operator/Customer.php(23): PleskX\Api\Client->request('<?xml version="...') #2 /var/www/vhosts/test.com/httpdocs/test.php(13): PleskX\Api\Operator\Customer->create(Array) #3 {main} thrown in /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Client.php on line 282
 
Thanks IgorG,

I afraid you have mis-understood what I mean. The error "User account already exists" is enough, but now it is showing too much: "
Fatal error: Uncaught PleskX\Api\Exception: User account already exists. in /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Client.php:282 Stack trace: #0 /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Client.php(161): PleskX\Api\Client->_verifyResponse(Object(PleskX\Api\XmlResponse)) #1 /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Operator/Customer.php(23): PleskX\Api\Client->request('<?xml version="...') #2 /var/www/vhosts/test.com/httpdocs/test.php(13): PleskX\Api\Operator\Customer->create(Array) #3 {main} thrown in /var/www/vhosts/test.com/httpdocs/vendor/plesk/api-php-lib/src/Api/Client.php on line 282:"

What I want is I can detect the result is success or error, and echo only the success/error msg (e.g. "User account already exists") .
 
@nethubonline

A simple thought : just include code to first check for the existence of the customer to be created - if exists, then return "error message".

You can also hook into the Plesk Api Exception and return that one message "User account already exists" - as @IgorG was trying to say.

Regards.........
 
Back
Top