PHP 异常未被捕获,laravel,流明

PHP exception not being caught, laravel, lumen

当试图 catch/handle 从 PDFParser 抛出的异常时,我无法捕获它。我使用一个简单的 try catch 语句,概述如下。

try{
  $pdf  = $parser->parseFile($filepath);
  $text = $pdf->getText();    
} catch( \Exception $e){
  $text = $paper->abstract;
}

抛出异常如下

if (empty($data)) {
        throw new \Exception('Object list not found. Possible secured file.');
}

The output is here.

lumen.ERROR: Exception: Object list not found. Possible secured file. in

/Users/pietrosette/Documents/CS_310/AcademicWordCloud-Backend/vendor/smalot/pdfparser/src/Smalot/PdfParser/Parser.php:98 Stack trace:

#0 /Users/pietrosette/Documents/CS_310/AcademicWordCloud-Backend/vendor/smalot/pdfparser/src/Smalot/PdfParser/Parser.php(74): Smalot\PdfParser\Parser->parseContent('%PDF-1.5\r%\xE2\xE3\xCF\xD3\r...')

#1 /Users/pietrosette/Documents/CS_310/AcademicWordCloud-Backend/app/Http/Controllers/ACMServer.php(198): Smalot\PdfParser\Parser->parseFile('/Users/pietrose...')

你的捕获有错别字:

try{
  $pdf  = $parser->parseFile($filepath);
  $text = $pdf->getText();    
} catch( \Execption $e){
  $text = $paper->abstract;
}

"Exception" 拼写错误。

我最终编写了一个包装器并返回了一个不同的值。这最终起作用了,实际上速度要快得多,因为它没有封装在 try/catch 块中。

    public function parseFile($filename)
    {
        $content = file_get_contents($filename);
        $return = @$this->parseContent($content);
        if($return === false ){
            return "PDF >= 1.5";
        } else{
            return $return;
        }
    }


    public function parseContent($content)
    {
        ...

        if (isset($xref['trailer']['encrypt'])) {
            return false;
        }

        if (empty($data)) {
            return false;
        }
        ....
   }

这导致将我的函数修改为以下内容。

    $pdf = $parser->parseFile($filepath);

    if($pdf === "PDF >= 1.5" ){
        $text = $abstract;
    } else  {
        $text = $pdf->getText();
    }