• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • We are looking for U.S.-based freelancer or agency working with SEO or WordPress for a quick 30-min interviews to gather feedback on XOVI, a successful German SEO tool we’re looking to launch in the U.S.
    If you qualify and participate, you’ll receive a $30 Amazon gift card as a thank-you. Please apply here. Thanks for helping shape a better SEO product for agencies!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

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