如何在php中处理OpenFire Restapi中的异常


How to handle the exception in OpenFire Restapi in php?

当前我在服务器中安装了openfire并开始使用它。我试图使用Restapi在openfire中创建一个用户。我得到了我所期望的产出。

现在,我尝试创建一个用户名为"abcdef"的用户,该用户已经存在。我想要一条消息,上面写着"用户名已经存在",但我得到了一个例外。

例外:

Uncaught exception 'GuzzleHttp'Exception'ClientException' with message 'Client error response [url] http://myip:9090/plugins/restapi/v1/users [status code] 409 [reason phrase] Conflict' in /var/www/html/open_fire_internal/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:88

来自异常的消息:

客户端错误响应[url]http://myip:9090/plugins/restapi/v1/users[状态代码]409[原因短语]冲突

堆栈跟踪:

#0 [...]/vendor/guzzlehttp/guzzle/src/Subscriber/HttpError.php(33): GuzzleHttp'Exception'RequestException::create(Object(GuzzleHttp'Message'Request), Object(GuzzleHttp'Message'Response))
#1 [...]/vendor/guzzlehttp/guzzle/src/Event/Emitter.php(109): GuzzleHttp'Subscriber'HttpError->onComplete(Object(GuzzleHttp'Event'CompleteEvent), 'complete')
#2 [...]/vendor/guzzlehttp/guzzle/src/RequestFsm.php(91): GuzzleHttp'Event'Emitter->emit('complete', Object(GuzzleHttp'Event'CompleteEvent))
#3 [...]/vendor/guzzlehttp/guzzle/src/RequestFsm.php(132): GuzzleHttp'RequestFsm->__invoke(Object(GuzzleHttp'Transaction))in /var/www/html/open_fire_internal/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 88

那么,有人能帮助我克服这个问题吗?

首先检查用户是否存在,然后添加

$user = $api->getuser($username);
if(!$user)
{
$result = $api->addUser('Username', 'Password', 'Real Name', 'email@email.tld', array('Group 1'));
// Check result if command is succesful
if($result) {
    // Display result, and check if it's an error or correct response
    echo ($result['result']) ? 'Success: ' : 'Error: ';
    echo $result['message'];
} else {
    // Something went wrong, probably connection issues
}
}
else
{
echo 'user already exists';
}