【问题标题】:Recover from "Fatal error: Uncaught SoapFault exception"从“致命错误:未捕获的 SoapFault 异常”中恢复
【发布时间】:2017-11-15 21:37:55
【问题描述】:

我已经有了函数式代码,但我目前正试图找到破解它的方法,以便我可以找到要捕获的异常。下面的代码连接到 Bluecat/Proteus API,并尝试根据通过 CSV 上传的 IP/MAC 组合列表分配 DHCP 预留。了解将使用此工具的人,IP 和/或 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
}
}
?>

我得到的具体错误是:

致命错误:未捕获的 SoapFault 异常:[env:Server] 八位字节值无效:C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php:30 中的 666 堆栈跟踪:#0 C:\Program文件 (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} 在第 30 行的 C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php 中抛出

SoapFault:无效的八位字节值:第 30 行 C:\Program Files (x86)\Ampps\www\proteustool\dhcp_reservations.php 中的 666

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

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

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

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

【问题讨论】:

  • 在处理之前验证地址?
  • 就是这么想的。将不得不弄清楚如何做到这一点,但这并不是什么大不了的事。这是我第一次使用 php 做任何事情,所以这是一种低优先级项目的在职学习。
  • 不难:if (filter_var($_SESSION['csv'][$i]['ip4Address'], FILTER_VALIDATE_IP)) { ...}
  • 不错。我只是在学习 php 的第 3 天,所以我还没有遇到过滤器。谢谢,这会做得很好!
  • 然后添加if (filter_var($_SESSION['csv'][$i]['macAddress'], FILTER_VALIDATE_MAC)) { ...}

标签: php soap fatal-error


【解决方案1】:
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
        }
    }

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

【讨论】:

    【解决方案2】:

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

    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>";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-19
      • 1970-01-01
      • 2018-12-24
      • 2015-05-23
      • 1970-01-01
      • 2015-10-15
      • 2012-02-18
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多