I'm using Laravel Excel v2.1.* for Laravel 5.1 to read data in from Excel files. I've come across fatal errors if the Excel file is badly formatted. My end users (who are reasonably proficient users of Excel) will need good feedback to point them in the right direction to clean up an Excel file before re-trying the routine which reads in the Excel data. The example I have is where a column is formatted to accept dates but in fact has cells containing the words "Yes" or "NO". When the method ExcelToPHPObject
the class PHPExcel_Shared_Date
(vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\Date.php
) attempts to create a DateTime object on line 168.
$dateObj = date_create('1-Jan-1970+'.$days.' days');
The $days
variable does not contain a valid value and $dataObj
is set to false
. This generates a fatal error with all the difficulty of adding some handling code to the App\Exceptions\Handler class. A workaround is to test $dateObj to see if it is false and throw a plain old Exception like this:
if($dateObj != false){
$dateObj->setTime($hours,$minutes,$seconds);
}else{
throw new Exception("Suitable error message here...");
}
This works well and the page where the user is attempting to import data from an Excel displays whatever message the developer wants to provide. However, I am very uncomfortable changing source code like this. Can you advise me how to handle fatal errors in this scenario. The Laravel application can't predict what kind of Excel file the user might attempt to read from and I would like a way of testing the file before reading it to avoid being caught out by fatal errors.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire