问题
When attempting to catch/handle an exception thrown from PDFParser, I cannot catch it. I use a simple try catch statement, outlined below.
try{
$pdf = $parser->parseFile($filepath);
$text = $pdf->getText();
} catch( \Exception $e){
$text = $paper->abstract;
}
The exception is thrown as follows.
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...')
回答1:
You have a typo in your catch:
try{
$pdf = $parser->parseFile($filepath);
$text = $pdf->getText();
} catch( \Execption $e){
$text = $paper->abstract;
}
"Exception" is spelled incorrectly.
回答2:
I ended up writing a wrapper and returning a different value. This ended up working and was actually quite a bit faster since it is not encapsulated in a try/catch block.
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;
}
....
}
Which lead to modifying my function to the following.
$pdf = $parser->parseFile($filepath);
if($pdf === "PDF >= 1.5" ){
$text = $abstract;
} else {
$text = $pdf->getText();
}
来源:https://stackoverflow.com/questions/43176364/php-exception-not-being-caught-laravel-lumen