流明5.6错误处理(抛出异常两次)

时间:2018-03-11 18:51:06

标签: php lumen laravel-5.6 lumen-5.5

这是我处理任何错误的代码:

应用\例外\处理程序::类

public function render($request, Exception $e)
{

    $fe = \Symfony\Component\Debug\Exception\FlattenException::create($e);

    $statusCode = $fe->getStatusCode();
    $code       = $fe->getCode();
    $message    = $fe->getMessage();

    $errorObj = new \App\LOHDomain\Entities\Error($code, $message);

    return response()->json(['data' => null, 'error' => $errorObj], $statusCode);
}

当我将一个伪造的WSDL URL解析为SoapClient时,它会抛出两个异常

{"data":null,"error":{"code":"0","message":"SOAP-ERROR: Parsing WSDL: Couldn't load from 'asdsd' : failed to load external entity \"asdsd\"\n"}}
{"data":null,"error":{"code":"1","message":"SOAP-ERROR: Parsing WSDL: Couldn't load from 'asdsd' : failed to load external entity \"asdsd\"\n"}}

所以json响应变得无效

在供应商中评论这些代码行时,会抛出一个例外:

Laravel \ Lumen \ Concerns \ RegistersExceptionHandlers trait

protected function registerErrorHandling()
{
        error_reporting(-1);

        set_error_handler(function ($level, $message, $file = '', $line = 0) {
            if (error_reporting() & $level) {
                throw new ErrorException($message, 0, $level, $file, $line);
            }
        });

        set_exception_handler(function ($e) {
            $this->handleUncaughtException($e);
        });

//        register_shutdown_function(function () {
//            $this->handleShutdown();
//        });
}

那么,问题是什么?以及如何解决它而无需在供应商中进行编辑?

1 个答案:

答案 0 :(得分:2)

解决方法是清除最后一个错误,因为它触发了两次。

  1. 错误例外。
  2. 第二个是关机功能。
  3. 所以,解决方案是:

    应用\例外\处理程序::类

    public function render($request, Exception $e)
    {
    
        $fe = \Symfony\Component\Debug\Exception\FlattenException::create($e);
    
        $statusCode = $fe->getStatusCode();
        $code       = $fe->getCode();
        $message    = $fe->getMessage();
    
        $errorObj = new \App\Domain\Entities\ResponseEntites\Error($code, $message);
    
        /**
         * This line of code resolves the issue
         * 
         * To reproduce the issue :
         * 1) Comment this following line of code
         * 2) Provide a fake WSDL URL to the SoapClient
         *
         * Recommendation: Remove this line if you aren't using the SoapClient
         */
        error_clear_last();
    
        return new \Illuminate\Http\JsonResponse(['data' => null, 'error' => $errorObj], $statusCode);
    }
    
      

    这不是最好的解决方案(但这是我尝试过的最佳解决方案)。   如果您有更好的测试解决方案,请分享。

    链接:

    1. Fatal exceptions are handled twice
    2. Code change