• Hi, Pleskians! We are running a UX testing of our upcoming product intended for server management and monitoring.
    We would like to invite you to have a call with us and have some fun checking our prototype. The agenda is pretty simple - we bring new design and some scenarios that you need to walk through and succeed. We will be watching and taking insights for further development of the design.
    If you would like to participate, please use this link to book a meeting. We will sent the link to the clickable prototype at the meeting.
  • Our UX team believes in the in the power of direct feedback and would like to invite you to participate in interviews, tests, and surveys.
    To stay in the loop and never miss an opportunity to share your thoughts, please subscribe to our UX research program. If you were previously part of the Plesk UX research program, please re-subscribe to continue receiving our invitations.
  • 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