Improve error reporting #752

This commit is contained in:
James Cole 2017-08-12 06:40:17 +02:00
parent 04a2cd1f1f
commit 31de86c9eb
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E

View File

@ -151,6 +151,43 @@ class CsvProcessor implements FileProcessorInterface
return $results; return $results;
} }
/**
* Will return string representation of JSON error code.
*
* @param int $jsonError
*
* @return string
*/
private function getJsonError(int $jsonError): string
{
switch ($jsonError) {
default:
return 'Unknown JSON error';
case JSON_ERROR_NONE:
return 'No JSON error';
case JSON_ERROR_DEPTH:
return 'JSON_ERROR_DEPTH';
case JSON_ERROR_STATE_MISMATCH:
return 'JSON_ERROR_STATE_MISMATCH';
case JSON_ERROR_CTRL_CHAR:
return 'JSON_ERROR_CTRL_CHAR';
case JSON_ERROR_SYNTAX:
return 'JSON_ERROR_SYNTAX';
case JSON_ERROR_UTF8:
return 'JSON_ERROR_UTF8';
case JSON_ERROR_RECURSION:
return 'JSON_ERROR_RECURSION';
case JSON_ERROR_INF_OR_NAN:
return 'JSON_ERROR_INF_OR_NAN';
case JSON_ERROR_UNSUPPORTED_TYPE:
return 'JSON_ERROR_UNSUPPORTED_TYPE';
case JSON_ERROR_INVALID_PROPERTY_NAME:
return 'JSON_ERROR_INVALID_PROPERTY_NAME';
case JSON_ERROR_UTF16:
return 'JSON_ERROR_UTF16';
}
}
/** /**
* Take a row, build import journal by annotating each value and storing it in the import journal. * Take a row, build import journal by annotating each value and storing it in the import journal.
* *
@ -158,14 +195,22 @@ class CsvProcessor implements FileProcessorInterface
* @param array $row * @param array $row
* *
* @return ImportJournal * @return ImportJournal
* @throws FireflyException
*/ */
private function importRow(int $index, array $row): ImportJournal private function importRow(int $index, array $row): ImportJournal
{ {
Log::debug(sprintf('Now at row %d', $index)); Log::debug(sprintf('Now at row %d', $index));
$row = $this->specifics($row); $row = $this->specifics($row);
$json = json_encode($row);
$jsonError = json_last_error();
if ($json === false) {
throw new FireflyException(sprintf('Error while encoding JSON: %s', $this->getJsonError($jsonError)));
}
$journal = new ImportJournal; $journal = new ImportJournal;
$journal->setUser($this->job->user); $journal->setUser($this->job->user);
$journal->setHash(hash('sha256', json_encode($row))); $journal->setHash(hash('sha256', $json));
foreach ($row as $rowIndex => $value) { foreach ($row as $rowIndex => $value) {
$value = trim($value); $value = trim($value);