Recover from "Fatal error: Uncaught SoapFault exception"

Recover from "Fatal error: Uncaught SoapFault exception"

我已经有了功能代码,但我目前正试图找到破解它的方法,以便我可以找到要捕获的异常。下面的代码连接到 Bluecat/Proteus API 并尝试针对通过 CSV 上传的 IP/MAC 组合列表分配 DHCP 预留。了解将使用此工具的人,IP and/or MAC 中的拼写错误是可能的,并且在给定的 CSV 上传中可能有数百个项目需要处理。

为了测试,我正在传递一个八位字节无效的 IP 和一个具有相同问题的 MAC。不幸的是,我遇到过这种错误类型,从我搜索的内容来看,没有办法优雅地捕获和记录错误并继续处理列表中的下一项。

这是我的代码:

<?php
session_start();
$client = new SoapClient("...server with WSDL thingy...");
$client->login($_SESSION['user'], $_SESSION['pass']);

# List of items to process taken from a form upload on a preceeding page
# List saved as array in 'csv'

foreach ($_SESSION['csv'] as $i=>$row) {

try {
$_SESSION['csv'][$i] = array_combine($_SESSION['keys'], $row);

$client->assignIP4Address('5',$_SESSION['csv'][$i]['ip4Address'],$_SESSION['csv'][$i]['macAddress'],'','MAKE_DHCP_RESERVED','');
}

catch (Exception $e) {
#Exception messaging goes here
}
}
?>

我遇到的具体错误是:

Fatal error: Uncaught SoapFault exception: [env:Server] Invalid octet value: 666 in C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php:30 Stack trace: #0 C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php(30): SoapClient->__call('assignIP4Addres...', Array) #1 C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php(30): SoapClient->assignIP4Address('5', '10.166.28.666', 'cc:dd:ee:ff:00:...', '', 'MAKE_DHCP_RESER...', '') #2 {main} thrown in C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php on line 30

SoapFault: Invalid octet value: 666 in C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php on line 30

请注意,该错误会终止位于无效 IP 的脚本,因为它在调用 soap 提供程序的方法的参数列表中排在第一位。我不知道无效 MAC 的错误是什么,但我猜是类似的东西。

无效的 IP/MAC 八位字节如下:x.x.x.666 和 xx:xx:xx:xx:xx:ZZ

有没有办法捕获这种特定类型的致命错误并继续处理 foreach 循环中的下一项?正如我所说,源 CSV 中可能有数百行,并且由于打字错误而让脚本在某个随机位置死掉会很痛苦。

这个问题是针对 PHP 5.6 的,只是为了完整。

foreach ($_SESSION['csv'] as $i=>$row){


        $_SESSION['csv'][$i] = array_combine($_SESSION['keys'],$row);

        if (filter_var($_SESSION['csv'][$i]['ip4Address'],FILTER_VALIDATE_IP) && filter_var($_SESSION['csv'][$i]['macAddress'],FILTER_VALIDATE_MAC)){
            $client->assignIP4Address('5',$_SESSION['csv'][$i]['ip4Address'],$_SESSION['csv'][$i]['macAddress'],'','MAKE_DHCP_RESERVED','');
        }else{
            // nothing actually it will just move on to then next row
            //you could record the faliur or alert the user if required
        }
    }

Try\catch 已删除,因为您似乎没有对它做任何事情

漂亮。我不得不添加更多的东西来测试实际的分配步骤,但它现在运行良好。这是我最后得到的结果,以防有人好奇:

foreach ($_SESSION['csv'] as $i=>$row){

$_SESSION['csv'][$i] = array_combine($_SESSION['keys'],$row);

if (filter_var($_SESSION['csv'][$i]['ip4Address'],FILTER_VALIDATE_IP) && filter_var($_SESSION['csv'][$i]['macAddress'],FILTER_VALIDATE_MAC)){

    $success = true;

    try {
        $client->assignIP4Address('5',$_SESSION['csv'][$i]['ip4Address'],$_SESSION['csv'][$i]['macAddress'],'','MAKE_DHCP_RESERVED','');
        }
    catch (Exception $e) {
        echo "Failure: " . $_SESSION['csv'][$i]['ip4Address'] . "/" . $_SESSION['csv'][$i]['macAddress'] . " could not be allocated. Cause: " . $e->getMessage() . "<br>";
        $success = false;
        }
    if ($success) {
        echo "Success: " . $_SESSION['csv'][$i]['ip4Address'] . " has been allocated to " . $_SESSION['csv'][$i]['macAddress'] . "<br>";
    }   
}
else{
    echo "Failure: " . $_SESSION['csv'][$i]['ip4Address'] . "/" . $_SESSION['csv'][$i]['macAddress'] . " could not be allocated. Please check that the IP or MAC is valid. <br>";
    }
}