2017-04-03 7 views
1

PDFParserからスローされた例外をキャッチ/処理しようとすると、私はそれをキャッチできません。私は、下記のような簡単なtry catchステートメントを使用します。PHP例外が捕捉されない、laravel、lumen

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...')

答えて

0

は、あなたのキャッチにタイプミスがあります

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

"例外" が間違って綴られています。

+0

編集していただきありがとうございますが、まだ動作しません。 –

+0

それはまだ同じミスですか? – suecarmol

+1

はい、私はそれに変わりました: if($ pdf === "PDF> ="){ $ text = "paper-> abstract"; } else { $ text = $ pdf-> getText(); }パブリック関数構文解析ファイル($ファイル名) {$コンテンツ=のfile_get_contents($ファイル名)のラッパーと 。 $ return = @ $ this-> parseContent($ content); if($ return === false){ return "PDF> = 1.5"; } else { return $ return; } } –

0

ラッパーを作成して別の値を返すことになりました。これは動作を終了し、実際にはかなり高速ですが、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(); 
    } 
関連する問題