Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Nov 23, 2024
1 parent 610096d commit 6389dcd
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/Console/AutoImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private function startImport(Configuration $configuration): void
// get files from disk:
if (!$disk->has($fileName)) {
SubmissionStatusManager::setSubmissionStatus(SubmissionStatus::SUBMISSION_ERRORED, $this->identifier);
$message = sprintf('File "%s" not found, cannot continue.', $fileName);
$message = sprintf('[a100]: File "%s" not found, cannot continue.', $fileName);
$this->error($message);
SubmissionStatusManager::addError($this->identifier, 0, $message);
$this->importMessages = $routine->getAllMessages();
Expand All @@ -469,7 +469,7 @@ private function startImport(Configuration $configuration): void
app('log')->debug(sprintf('Found %d transactions on the drive.', count($transactions)));
} catch (FileNotFoundException|\JsonException $e) {
SubmissionStatusManager::setSubmissionStatus(SubmissionStatus::SUBMISSION_ERRORED, $this->identifier);
$message = sprintf('File "%s" could not be decoded, cannot continue..', $fileName);
$message = sprintf('[a101]: File "%s" could not be decoded, cannot continue..', $fileName);
$this->error($message);
SubmissionStatusManager::addError($this->identifier, 0, $message);
$this->importMessages = $routine->getAllMessages();
Expand Down
43 changes: 32 additions & 11 deletions app/Console/Commands/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class Import extends Command
*
* @var string
*/
protected $signature = 'importer:import
protected $signature = 'importer:import
{config : The configuration file. }
{file? : Optionally, the importable file you want to import}
';
Expand All @@ -66,7 +66,7 @@ final class Import extends Command
*/
public function handle(): int
{
$access = $this->haveAccess();
$access = $this->haveAccess();
if (false === $access) {
$this->error(sprintf('No access granted, or no connection is possible to your local Firefly III instance at %s.', config('importer.url')));
app('log')->error(sprintf('Exit code is %s.', ExitCode::NO_CONNECTION->name));
Expand All @@ -76,8 +76,8 @@ public function handle(): int

$this->info(sprintf('Welcome to the Firefly III data importer, v%s', config('importer.version')));
app('log')->debug(sprintf('Now in %s', __METHOD__));
$file = (string) $this->argument('file');
$config = (string) $this->argument('config'); // @phpstan-ignore-line
$file = (string) $this->argument('file');
$config = (string) $this->argument('config'); // @phpstan-ignore-line

// validate config path:
if ('' !== $config) {
Expand Down Expand Up @@ -110,7 +110,7 @@ public function handle(): int
return ExitCode::CANNOT_READ_CONFIG->value;
}

$jsonResult = $this->verifyJSON($config);
$jsonResult = $this->verifyJSON($config);
if (false === $jsonResult) {
$message = 'The importer can\'t import: could not decode the JSON in the config file.';
$this->error($message);
Expand Down Expand Up @@ -142,11 +142,18 @@ public function handle(): int
$this->reportConversion();

// crash here if the conversion failed.
$exitCode = ExitCode::SUCCESS->value;
$exitCode = ExitCode::SUCCESS->value;
if (0 !== count($this->conversionErrors)) {
$this->error('There are many errors in the data conversion. The import will stop here.');
$exitCode = ExitCode::TOO_MANY_ERRORS_PROCESSING->value;
app('log')->error(sprintf('Exit code is %s.', ExitCode::TOO_MANY_ERRORS_PROCESSING->name));
$exitCode = ExitCode::TOO_MANY_ERRORS_PROCESSING->value;
// could still be that there were simply no transactions (from GoCardless). This can result
// in another exit code.
if($this->isNothingDownloaded()) {
app('log')->error(sprintf('Exit code changed to %s.', ExitCode::NOTHING_WAS_IMPORTED->name));
$exitCode = ExitCode::NOTHING_WAS_IMPORTED->value;
}

$this->error('There are many errors in the data conversion. The import will stop here.');
}
if (0 === count($this->conversionErrors)) {
$this->line(sprintf('Done converting from file %s using configuration %s.', $file, $config));
Expand All @@ -158,9 +165,9 @@ public function handle(): int
$this->reportBalanceDifferences($configuration);

// merge things:
$messages = array_merge($this->importMessages, $this->conversionMessages);
$warnings = array_merge($this->importWarnings, $this->conversionWarnings);
$errors = array_merge($this->importErrors, $this->conversionErrors);
$messages = array_merge($this->importMessages, $this->conversionMessages);
$warnings = array_merge($this->importWarnings, $this->conversionWarnings);
$errors = array_merge($this->importErrors, $this->conversionErrors);

event(new ImportedTransactions($messages, $warnings, $errors, $this->conversionRateLimits));
if (0 !== count($this->importErrors)) {
Expand All @@ -177,4 +184,18 @@ public function handle(): int

return $exitCode;
}

private function isNothingDownloaded(): bool
{
/** @var array $errors */
foreach ($this->conversionErrors as $errors) {
/** @var string $error */
foreach ($errors as $error) {
if (str_contains($error, '[a111]')) {
return true;
}
}
}
return false;
}
}
6 changes: 3 additions & 3 deletions app/Services/CSV/Conversion/Routine/CSVFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function processCSVFile(): array
} catch (Exception $e) {
app('log')->error($e->getMessage());
// app('log')->error($e->getTraceAsString());
$message = sprintf('Could not set delimiter: %s', $e->getMessage());
$message = sprintf('[a106]: Could not set delimiter: %s', $e->getMessage());
$this->addError(0, $message);

return [];
Expand All @@ -78,7 +78,7 @@ public function processCSVFile(): array
} catch (Exception $e) {
app('log')->error($e->getMessage());
// app('log')->error($e->getTraceAsString());
$message = sprintf('Could not read CSV: %s', $e->getMessage());
$message = sprintf('[a107]: Could not read CSV: %s', $e->getMessage());
$this->addError(0, $message);

return [];
Expand All @@ -89,7 +89,7 @@ public function processCSVFile(): array
} catch (ImporterErrorException $e) {
app('log')->error($e->getMessage());
// app('log')->error($e->getTraceAsString());
$message = sprintf('Could not parse CSV: %s', $e->getMessage());
$message = sprintf('[a108]: Could not parse CSV: %s', $e->getMessage());
$this->addError(0, $message);

return [];
Expand Down
2 changes: 1 addition & 1 deletion app/Services/CSV/Conversion/RoutineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function start(): array
// $this->addWarning(7, '7: No transactions found in CSV file.');

if (0 === $count) {
$this->addError(0, 'No transactions found in CSV file.');
$this->addError(0, '[a105]: No transactions found in CSV file.');
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand Down
6 changes: 3 additions & 3 deletions app/Services/Camt/Conversion/RoutineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function start(): array
$camtMessage = $this->getCamtMessage();
if (null === $camtMessage) {
app('log')->error('The CAMT object is NULL, probably due to a previous error');
$this->addError(0, 'The CAMT object is NULL, probably due to a previous error');
$this->addError(0, '[a102]: The CAMT object is NULL, probably due to a previous error');
// at this point there are very few (if not zero) errors from other steps in the routine.
// Still: merge errors so they can be reported to the user:
$this->mergeMessages(1);
Expand All @@ -124,7 +124,7 @@ public function start(): array

if (0 === count($transactions)) {
app('log')->error('No transactions found in CAMT file');
$this->addError(0, 'No transactions found in CAMT file.');
$this->addError(0, '[a103]: No transactions found in CAMT file.');

$this->mergeMessages(1);
$this->mergeWarnings(1);
Expand Down Expand Up @@ -158,7 +158,7 @@ private function getCamtMessage(): ?Message
} catch (InvalidMessageException $e) {
app('log')->error('Conversion error in RoutineManager::getCamtMessage');
app('log')->error($e->getMessage());
$this->addError(0, sprintf('Could not convert CAMT.053 file: %s', $e->getMessage()));
$this->addError(0, sprintf('[a104]: Could not convert CAMT.053 file: %s', $e->getMessage()));

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function download(): array
} catch (AgreementExpiredException $e) {
$this->addError(
0,
'Your GoCardless End User Agreement has expired. You must refresh it by generating a new one through the Firefly III Data Importer user interface. See the other error messages for more information.'
'[a113]: Your GoCardless End User Agreement has expired. You must refresh it by generating a new one through the Firefly III Data Importer user interface. See the other error messages for more information.'
);
if (array_key_exists('summary', $e->json) && '' !== (string) $e->json['summary']) {
$this->addError(0, $e->json['summary']);
Expand Down Expand Up @@ -133,7 +133,7 @@ public function download(): array
app('log')->debug(sprintf('Ran into %s instead of GetTransactionsResponse', get_class($e)));
// agreement expired, whoops.
$return[$account] = [];
$this->addError(0, $e->json['detail'] ?? 'Your EUA has expired.');
$this->addError(0, $e->json['detail'] ?? '[a114]: Your EUA has expired.');
// save rate limits, even though they may not be there.
$this->rateLimits[$account] = [
'remaining' => $request->getRemaining(),
Expand Down
8 changes: 4 additions & 4 deletions app/Services/Nordigen/Conversion/RoutineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private function downloadFromGoCardless(): void
app('log')->error($e->getMessage());

// add error to current error thing:
$this->addError(0, sprintf('Could not download from GoCardless: %s', $e->getMessage()));
$this->addError(0, sprintf('[a109]: Could not download from GoCardless: %s', $e->getMessage()));
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand Down Expand Up @@ -275,7 +275,7 @@ private function collectTargetAccounts(): void
try {
$this->transactionGenerator->collectTargetAccounts();
} catch (ApiHttpException $e) {
$this->addError(0, sprintf('Error while collecting target accounts: %s', $e->getMessage()));
$this->addError(0, sprintf('[a110]: Error while collecting target accounts: %s', $e->getMessage()));
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand Down Expand Up @@ -328,7 +328,7 @@ private function breakOnDownload(): bool
if (0 === $total) {
app('log')->warning('Downloaded nothing, will return nothing.');
// add error to current error thing:
$this->addError(0, 'No transactions were downloaded from GoCardless. You may be rate limited or something else went wrong.');
$this->addError(0, '[a111]: No transactions were downloaded from GoCardless. You may be rate limited or something else went wrong.');
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand All @@ -347,7 +347,7 @@ private function collectGoCardlessAccounts(): void
app('log')->error('Could not collect info on all GoCardless accounts, but this info isn\'t used at the moment anyway.');
app('log')->error($e->getMessage());
} catch (AgreementExpiredException $e) {
$this->addError(0, 'The connection between your bank and GoCardless has expired.');
$this->addError(0, '[a112]: The connection between your bank and GoCardless has expired.');
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand Down
14 changes: 7 additions & 7 deletions app/Services/Shared/Import/Routine/ApiSubmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function uniqueTransaction(int $index, array $line): ?bool
sprintf('Looks like field "%s" with value "%s" is not unique, found in group #%d. Return false', $field, $value, $searchResult)
);
$message = sprintf(
'There is already a transaction with %s "%s" (<a href="%s/transactions/show/%d">link</a>).',
'[a115]: There is already a transaction with %s "%s" (<a href="%s/transactions/show/%d">link</a>).',
$field,
$value,
$this->vanityURL,
Expand Down Expand Up @@ -258,7 +258,7 @@ private function processTransaction(int $index, array $line): array

return $return;
}
$message = sprintf('Submission HTTP error: %s', e($e->getMessage()));
$message = sprintf('[a116]: Submission HTTP error: %s', e($e->getMessage()));
app('log')->error($e->getMessage());
$this->addError($index, $message);

Expand All @@ -269,7 +269,7 @@ private function processTransaction(int $index, array $line): array
foreach ($response->errors->messages() as $key => $errors) {
app('log')->error(sprintf('Submission error: %d', $key), $errors);
foreach ($errors as $error) {
$msg = sprintf('%s: %s (original value: "%s")', $key, $error, $this->getOriginalValue($key, $line));
$msg = sprintf('[a117]: %s: %s (original value: "%s")', $key, $error, $this->getOriginalValue($key, $line));
if (false === $this->isDuplicationError($key, $error) || false === config('importer.ignore_duplicate_errors')) {
$this->addError($index, $msg);
}
Expand All @@ -284,7 +284,7 @@ private function processTransaction(int $index, array $line): array
/** @var TransactionGroup $group */
$group = $response->getTransactionGroup();
if (null === $group) {
$message = 'Could not create transaction. Unexpected empty response from Firefly III. Check the logs.';
$message = '[a118]: Could not create transaction. Unexpected empty response from Firefly III. Check the logs.';
app('log')->error($message, $response->getRawData());
$this->addError($index, $message);

Expand All @@ -293,7 +293,7 @@ private function processTransaction(int $index, array $line): array

// perhaps zero transactions in the array.
if (0 === count($group->transactions)) {
$message = 'Could not create transaction. Transaction-count from Firefly III is zero. Check the logs.';
$message = '[a119]: Could not create transaction. Transaction-count from Firefly III is zero. Check the logs.';
app('log')->error($message, $response->getRawData());
$this->addError($index, $message);

Expand Down Expand Up @@ -495,7 +495,7 @@ private function addTagToGroups(array $groupInfo): void
} catch (ApiHttpException $e) {
app('log')->error($e->getMessage());
// app('log')->error($e->getTraceAsString());
$this->addError(0, 'Could not store transaction: see the log files.');
$this->addError(0, '[a120]: Could not store transaction: see the log files.');
}
app('log')->debug(sprintf('Added import tag to transaction group #%d', $groupId));
}
Expand All @@ -522,7 +522,7 @@ private function createTag(): void
/** @var PostTagResponse $response */
$response = $request->post();
} catch (ApiHttpException $e) {
$message = sprintf('Could not create tag. %s', $e->getMessage());
$message = sprintf('[a121]: Could not create tag. %s', $e->getMessage());
app('log')->error($message);
// app('log')->error($e->getTraceAsString());
$this->addError(0, $message);
Expand Down
4 changes: 2 additions & 2 deletions app/Services/Spectre/Conversion/RoutineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function start(): array
try {
$this->transactionGenerator->collectTargetAccounts();
} catch (ApiHttpException $e) {
$this->addError(0, sprintf('Cannot download Spectre accounts: %s', $e->getMessage()));
$this->addError(0, sprintf('[a122]: Cannot download Spectre accounts: %s', $e->getMessage()));
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand All @@ -114,7 +114,7 @@ public function start(): array
$converted = $this->transactionGenerator->getTransactions($transactions);
app('log')->debug(sprintf('Generated %d Firefly III transactions.', count($converted)));
if (0 === count($converted)) {
$this->addError(0, 'No transactions were converted, probably zero found at Spectre.');
$this->addError(0, '[a123]: No transactions were converted, probably zero found at Spectre.');
$this->mergeMessages(1);
$this->mergeWarnings(1);
$this->mergeErrors(1);
Expand Down

0 comments on commit 6389dcd

Please sign in to comment.