Error handling
Every parse method has the same contract: parse() returns one transaction or throws, tryParse() returns null instead of throwing a parse error, and parseAll() returns every transaction or throws. This recipe runs all three on four mails that cannot be read as one payment, then shows Money refusing amounts it would have to guess.
Source: examples/error-handling
The mails
| File | Problem |
|---|---|
not-a-bank.txt | A parcel notice. No parser supports it |
two-transactions.txt | A valid ČSOB CZ mail with two transactions |
broken-balance.txt | The same mail with the second balance changed, so the balances no longer add up |
card-result-two-amounts.txt | A CardPay result that contains AMT twice with different values |
broken-balance.txt
text
Vážený kliente,
toto je automaticky generovaný e-mail ze služby ČSOB CEB, neodpovídejte na něj.
Dne 25.9.2018 byla na účtu 123456789 zaúčtovaná transakce typu: Došlá platba.
Název smlouvy: CRM International a.s.
Číslo smlouvy: 87654321
Majitel smlouvy: Shmelina a.s.
Účet: 123456789, CZK, CRM INTERNATION
Částka: +1 234,56 CZK
Účet protistrany: 1122334455/9999
Název protistrany: Capi Hnizdo a.s.
Variabilní symbol: 23456789
Konstantní symbol: 3456
Zůstatek na účtu po zaúčtování transakce: +1 234 567,89 CZK.
Dne 25.9.2018 byla na účtu 123456789 zaúčtovaná transakce typu: Došlá platba.
Název smlouvy: CRM International a.s.
Číslo smlouvy: 87654321
Majitel smlouvy: Shmelina a.s.
Účet: 123456789, CZK, CRM INTERNATION
Částka: +987,65 CZK
Účet protistrany: 9988776655/1111
Název protistrany: Sorry jako a.s.
Variabilní symbol: 78787878
Konstantní symbol: 6789
Zůstatek na účtu po zaúčtování transakce: +1 235 555,00 CZK.
S přáním krásného dne
Vaše ČSOBtext
AMT=10.00 CURR=978 VS=1234567890 RES=OK AC=123456 AMT=1000.00 TID=88888888 TIMESTAMP=25112016223023 HMAC=9dfd46cf2af977be8dd4251f4ef92307d95a8903f4738616d8456bd02e858340The code
php
<?php
declare(strict_types=1);
// What each parse method does with a mail it cannot read. parse() throws, tryParse() returns null,
// parseAll() returns every transaction. Every exception implements BankMailsParserException.
require __DIR__ . '/vendor/autoload.php';
use Tomaj\BankMailsParser\BankMailsParser;
use Tomaj\BankMailsParser\Exception\BankMailsParserException;
use Tomaj\BankMailsParser\Exception\InvalidArgumentException;
use Tomaj\BankMailsParser\ValueObject\Money;
$parser = new BankMailsParser();
$mail = static fn(string $file): string => (string) file_get_contents(__DIR__ . '/mails/' . $file);
$cases = [
// [file, sender]
['not-a-bank.txt', null],
['two-transactions.txt', 'info@csob.cz'],
['broken-balance.txt', 'info@csob.cz'],
['card-result-two-amounts.txt', 'cardpay@tatrabanka.sk'],
];
foreach ($cases as [$file, $sender]) {
echo $file, "\n";
try {
$result = $parser->parse($mail($file), $sender);
echo ' parse(): ', $result->money?->format(), "\n";
} catch (BankMailsParserException $e) {
echo ' parse(): ', new ReflectionClass($e)->getShortName(), ': ', $e->getMessage(), "\n";
}
$result = $parser->tryParse($mail($file), $sender);
echo ' tryParse(): ', $result === null ? 'null' : $result->money?->format(), "\n";
try {
$all = $parser->parseAll($mail($file), $sender);
echo ' parseAll(): ', implode(', ', array_map(static fn($t): string => (string) $t->money?->format(), $all)), "\n";
} catch (BankMailsParserException $e) {
echo ' parseAll(): ', new ReflectionClass($e)->getShortName(), "\n";
}
echo "\n";
}
// Money never guesses: "1.234" could be 1.234 or 1234, so it is rejected without a decimal separator.
foreach ([['1 234,56', null], ['1.234', null], ['1.234', ','], ['12.345', '.']] as [$amount, $separator]) {
try {
echo "Money '{$amount}' (separator ", $separator ?? 'auto', '): ', Money::fromDecimalString($amount, 'EUR', $separator)->format(), "\n";
} catch (InvalidArgumentException $e) {
echo 'InvalidArgumentException: ', $e->getMessage(), "\n";
}
}The output
text
not-a-bank.txt
parse(): UnsupportedBankException: No parser found that supports this email format. The email may be from an unsupported bank or have an unrecognized format.
tryParse(): null
parseAll(): UnsupportedBankException
two-transactions.txt
parse(): MultipleTransactionsException: Email contains 2 transactions. Use parseAll().
tryParse(): null
parseAll(): 1234.56 CZK, 987.65 CZK
broken-balance.txt
parse(): InvalidEmailFormatException: Parser Tomaj\BankMailsParser\Parser\Csob\CsobMailParser: Balance does not match the previous transaction
tryParse(): null
parseAll(): InvalidEmailFormatException
card-result-two-amounts.txt
parse(): InvalidEmailFormatException: Parser Tomaj\BankMailsParser\Parser\TatraBanka\TatraBankaSimpleMailParser: Duplicate CardPay field amount
tryParse(): null
parseAll(): InvalidEmailFormatException
Money '1 234,56' (separator auto): 1234.56 EUR
Money '1.234' (separator auto): InvalidArgumentException: Ambiguous decimal amount "1.234".
Money '1.234' (separator ,): 1234.00 EUR
Money '12.345' (separator .): InvalidArgumentException: Amount "12.345" has more fraction digits than the currency allows.Reading the result
| Situation | parse() | tryParse() | parseAll() |
|---|---|---|---|
| No parser supports the mail | UnsupportedBankException | null | UnsupportedBankException |
| Two parsers support it | AmbiguousBankException | null | AmbiguousBankException |
| Several transactions | MultipleTransactionsException | null | every transaction |
| A bank mail with broken or contradicting fields | InvalidEmailFormatException | null | InvalidEmailFormatException |
| Missing key file or wrong passphrase for a statement | ConfigurationException | ConfigurationException | ConfigurationException |
- All of them implement
Tomaj\BankMailsParser\Exception\BankMailsParserException, so onecatchtakes everything from the package. The parse errors extendParseException;ConfigurationExceptionandInvalidArgumentExceptiondo not. - The broken balance chain is the library refusing a mail where a payer could have typed a fake transaction into a message: when the bank's balance lines do not confirm the amounts, nothing from that mail is returned.
Money::fromDecimalString()without a decimal separator rejects1.234: it could be 1.234 or 1234. Parsers always pass the bank's separator.12.345with.as the separator has three fraction digits, more than EUR allows, and is rejected rather than rounded. Both throwTomaj\BankMailsParser\Exception\InvalidArgumentException, which a parser turns intoInvalidEmailFormatException.
See Exceptions for the full hierarchy.