• We value your experience with Plesk during 2024
    Plesk strives to perform even better in 2025. To help us improve further, please answer a few questions about your experience with Plesk Obsidian 2024.
    Please take this short survey:

    https://pt-research.typeform.com/to/AmZvSXkx
  • 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.

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