Catch various errors.

This commit is contained in:
James Cole
2018-07-26 06:27:52 +02:00
parent 6bcfea1de4
commit 89be30c4b9
19 changed files with 76 additions and 50 deletions

View File

@@ -88,8 +88,9 @@ class RegisterController extends Controller
session()->flash('success', (string)trans('firefly.registered')); session()->flash('success', (string)trans('firefly.registered'));
return $this->registered($request, $user) $this->registered($request, $user);
?: redirect($this->redirectPath());
return redirect($this->redirectPath());
} }
/** /**

View File

@@ -25,8 +25,6 @@ namespace FireflyIII\Services\Bunq;
use bunq\Context\ApiContext as BunqApiContext; use bunq\Context\ApiContext as BunqApiContext;
use bunq\Context\BunqContext; use bunq\Context\BunqContext;
use bunq\Exception\BadRequestException;
use bunq\Exception\BunqException;
use bunq\Util\BunqEnumApiEnvironmentType; use bunq\Util\BunqEnumApiEnvironmentType;
use Exception; use Exception;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
@@ -39,6 +37,7 @@ use Log;
*/ */
class ApiContext class ApiContext
{ {
/** @noinspection MoreThanThreeArgumentsInspection */
/** /**
* @param BunqEnumApiEnvironmentType $environmentType * @param BunqEnumApiEnvironmentType $environmentType
* @param string $apiKey * @param string $apiKey
@@ -50,11 +49,11 @@ class ApiContext
* @return BunqApiContext * @return BunqApiContext
*/ */
public function create(BunqEnumApiEnvironmentType $environmentType, string $apiKey, string $description, array $permittedIps, string $proxyUrl = null public function create(BunqEnumApiEnvironmentType $environmentType, string $apiKey, string $description, array $permittedIps, string $proxyUrl = null
) { ): BunqApiContext {
$permittedIps = $permittedIps ?? []; $permittedIps = $permittedIps ?? [];
try { try {
$context = BunqApiContext::create($environmentType, $apiKey, $description, $permittedIps, $proxyUrl); $context = BunqApiContext::create($environmentType, $apiKey, $description, $permittedIps, $proxyUrl);
} catch (BunqException|BadRequestException|Exception $e) { } catch (Exception $e) {
$message = $e->getMessage(); $message = $e->getMessage();
Log::error($message); Log::error($message);
Log::error($e->getTraceAsString()); Log::error($e->getTraceAsString());
@@ -78,7 +77,7 @@ class ApiContext
try { try {
$apiContext = BunqApiContext::fromJson($jsonString); $apiContext = BunqApiContext::fromJson($jsonString);
BunqContext::loadApiContext($apiContext); BunqContext::loadApiContext($apiContext);
} catch (BadRequestException|BunqException|Exception $e) { } catch (Exception $e) {
$message = $e->getMessage(); $message = $e->getMessage();
Log::error($message); Log::error($message);
Log::error($e->getTraceAsString()); Log::error($e->getTraceAsString());

View File

@@ -23,7 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Bunq; namespace FireflyIII\Services\Bunq;
use bunq\Exception\BunqException;
use bunq\Model\Generated\Endpoint\BunqResponseMonetaryAccountList; use bunq\Model\Generated\Endpoint\BunqResponseMonetaryAccountList;
use bunq\Model\Generated\Endpoint\MonetaryAccount as BunqMonetaryAccount; use bunq\Model\Generated\Endpoint\MonetaryAccount as BunqMonetaryAccount;
use Exception; use Exception;
@@ -47,7 +47,7 @@ class MonetaryAccount
$customHeaders = $customHeaders ?? []; $customHeaders = $customHeaders ?? [];
try { try {
$result = BunqMonetaryAccount::listing($params, $customHeaders); $result = BunqMonetaryAccount::listing($params, $customHeaders);
} catch (BunqException|Exception $e) { } catch (Exception $e) {
throw new FireflyException($e->getMessage()); throw new FireflyException($e->getMessage());
} }

View File

@@ -23,7 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Bunq; namespace FireflyIII\Services\Bunq;
use bunq\Exception\BunqException;
use bunq\Model\Generated\Endpoint\BunqResponsePaymentList; use bunq\Model\Generated\Endpoint\BunqResponsePaymentList;
use bunq\Model\Generated\Endpoint\Payment as BunqPayment; use bunq\Model\Generated\Endpoint\Payment as BunqPayment;
use Exception; use Exception;
@@ -49,7 +49,7 @@ class Payment
$customHeaders = $customHeaders ?? []; $customHeaders = $customHeaders ?? [];
try { try {
$result = BunqPayment::listing($monetaryAccountId, $params, $customHeaders); $result = BunqPayment::listing($monetaryAccountId, $params, $customHeaders);
} catch (BunqException|Exception $e) { } catch (Exception $e) {
throw new FireflyException($e->getMessage()); throw new FireflyException($e->getMessage());
} }

View File

@@ -74,17 +74,19 @@ class FixerIOv2 implements ExchangeRateInterface
); );
$statusCode = -1; $statusCode = -1;
Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri))); Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri)));
try {
$client = new Client; $client = new Client;
try {
$res = $client->request('GET', $uri); $res = $client->request('GET', $uri);
$statusCode = $res->getStatusCode(); $statusCode = $res->getStatusCode();
$body = $res->getBody()->getContents(); $body = $res->getBody()->getContents();
Log::debug(sprintf('Result status code is %d', $statusCode));
Log::debug(sprintf('Result body is: %s', $body));
} catch (GuzzleException|Exception $e) { } catch (GuzzleException|Exception $e) {
// don't care about error // don't care about error
$body = sprintf('Guzzle exception: %s', $e->getMessage()); $body = sprintf('Guzzle exception: %s', $e->getMessage());
$statusCode = 500;
} }
Log::debug(sprintf('Result status code is %d', $statusCode));
Log::debug(sprintf('Result body is: %s', $body));
$content = null; $content = null;
if (200 !== $statusCode) { if (200 !== $statusCode) {
@@ -103,7 +105,7 @@ class FixerIOv2 implements ExchangeRateInterface
} }
$exchangeRate->rate = $rate; $exchangeRate->rate = $rate;
if ($rate !== 0.0) { if (0.0 !== $rate) {
Log::debug('Rate is not zero, save it!'); Log::debug('Rate is not zero, save it!');
$exchangeRate->save(); $exchangeRate->save();
} }

View File

@@ -42,20 +42,27 @@ class RecurrenceDestroyService
try { try {
// delete all meta data // delete all meta data
$recurrence->recurrenceMeta()->delete(); $recurrence->recurrenceMeta()->delete();
} catch (Exception $e) { // @codeCoverageIgnore
Log::info(sprintf('Could not delete recurrence meta: %s', $e->getMessage())); // @codeCoverageIgnore
}
// delete all transactions. // delete all transactions.
/** @var RecurrenceTransaction $transaction */ /** @var RecurrenceTransaction $transaction */
foreach($recurrence->recurrenceTransactions as $transaction) { foreach ($recurrence->recurrenceTransactions as $transaction) {
$transaction->recurrenceTransactionMeta()->delete(); $transaction->recurrenceTransactionMeta()->delete();
try {
$transaction->delete(); $transaction->delete();
} catch (Exception $e) { // @codeCoverageIgnore
Log::info(sprintf('Could not delete recurrence transaction: %s', $e->getMessage())); // @codeCoverageIgnore
}
} }
// delete all repetitions // delete all repetitions
$recurrence->recurrenceRepetitions()->delete(); $recurrence->recurrenceRepetitions()->delete();
// delete recurrence // delete recurrence
try {
$recurrence->delete(); $recurrence->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @codeCoverageIgnore
Log::error(sprintf('Could not delete recurrence: %s', $e->getMessage())); // @codeCoverageIgnore Log::info(sprintf('Could not delete recurrence: %s', $e->getMessage())); // @codeCoverageIgnore
} }
} }

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Support; namespace FireflyIII\Services\Internal\Support;
use Exception;
use FireflyIII\Factory\AccountFactory; use FireflyIII\Factory\AccountFactory;
use FireflyIII\Factory\AccountMetaFactory; use FireflyIII\Factory\AccountMetaFactory;
use FireflyIII\Factory\TransactionFactory; use FireflyIII\Factory\TransactionFactory;
@@ -80,7 +81,7 @@ trait AccountServiceTrait
* *
* @return null|string * @return null|string
*/ */
public function filterIban(?string $iban) public function filterIban(?string $iban): ?string
{ {
if (null === $iban) { if (null === $iban) {
return null; return null;
@@ -204,7 +205,9 @@ trait AccountServiceTrait
'reconciled' => false, 'reconciled' => false,
] ]
); );
if (null !== $one && null !== $two) {
Log::notice(sprintf('Stored two transactions for new account, #%d and #%d', $one->id, $two->id)); Log::notice(sprintf('Stored two transactions for new account, #%d and #%d', $one->id, $two->id));
}
return $journal; return $journal;
} }
@@ -214,6 +217,7 @@ trait AccountServiceTrait
* @param string $name * @param string $name
* *
* @return Account * @return Account
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function storeOpposingAccount(User $user, string $name): Account public function storeOpposingAccount(User $user, string $name): Account
{ {
@@ -231,6 +235,7 @@ trait AccountServiceTrait
* @param array $data * @param array $data
* *
* @return bool * @return bool
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function updateIB(Account $account, array $data): bool public function updateIB(Account $account, array $data): bool
{ {
@@ -313,14 +318,14 @@ trait AccountServiceTrait
* @param Account $account * @param Account $account
* @param array $data * @param array $data
*/ */
public function updateMetaData(Account $account, array $data) public function updateMetaData(Account $account, array $data): void
{ {
$fields = $this->validFields; $fields = $this->validFields;
if ($account->accountType->type === AccountType::ASSET) { if ($account->accountType->type === AccountType::ASSET) {
$fields = $this->validAssetFields; $fields = $this->validAssetFields;
} }
if ($account->accountType->type === AccountType::ASSET && $data['accountRole'] === 'ccAsset') { if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['accountRole']) {
$fields = $this->validCCFields; $fields = $this->validCCFields;
} }
/** @var AccountMetaFactory $factory */ /** @var AccountMetaFactory $factory */
@@ -346,8 +351,12 @@ trait AccountServiceTrait
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $data[$field], $account->id, $account->name)); Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $data[$field], $account->id, $account->name));
} }
} }
if (null !== $entry && isset($data[$field]) && \strlen((string)$data[$field]) === 0) { if (null !== $entry && isset($data[$field]) && '' === (string)$data[$field]) {
try {
$entry->delete(); $entry->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage()));
}
} }
} }
} }
@@ -360,7 +369,7 @@ trait AccountServiceTrait
*/ */
public function updateNote(Account $account, string $note): bool public function updateNote(Account $account, string $note): bool
{ {
if (0 === \strlen($note)) { if ('' === $note) {
$dbNote = $account->notes()->first(); $dbNote = $account->notes()->first();
if (null !== $dbNote) { if (null !== $dbNote) {
$dbNote->delete(); $dbNote->delete();

View File

@@ -67,7 +67,7 @@ trait BillServiceTrait
*/ */
public function updateNote(Bill $bill, string $note): bool public function updateNote(Bill $bill, string $note): bool
{ {
if (0 === \strlen($note)) { if ('' === $note) {
$dbNote = $bill->notes()->first(); $dbNote = $bill->notes()->first();
if (null !== $dbNote) { if (null !== $dbNote) {
$dbNote->delete(); // @codeCoverageIgnore $dbNote->delete(); // @codeCoverageIgnore

View File

@@ -53,9 +53,11 @@ trait JournalServiceTrait
foreach ($data['tags'] as $string) { foreach ($data['tags'] as $string) {
if (\strlen($string) > 0) { if (\strlen($string) > 0) {
$tag = $factory->findOrCreate($string); $tag = $factory->findOrCreate($string);
if (null !== $tag) {
$set[] = $tag->id; $set[] = $tag->id;
} }
} }
}
$journal->tags()->sync($set); $journal->tags()->sync($set);
} }

View File

@@ -205,7 +205,7 @@ trait RecurringTransactionTrait
$entry->value = implode(',', $tags); $entry->value = implode(',', $tags);
$entry->save(); $entry->save();
} }
if (\count($tags) === 0) { if (0 === \count($tags)) {
// delete if present // delete if present
$recurrence->recurrenceMeta()->where('name', 'tags')->delete(); $recurrence->recurrenceMeta()->where('name', 'tags')->delete();
} }

View File

@@ -100,6 +100,7 @@ trait TransactionServiceTrait
* @param string|null $accountName * @param string|null $accountName
* *
* @return Account|null * @return Account|null
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account
{ {

View File

@@ -41,17 +41,18 @@ class AccountUpdateService
* @param array $data * @param array $data
* *
* @return Account * @return Account
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function update(Account $account, array $data): Account public function update(Account $account, array $data): Account
{ {
// update the account itself: // update the account itself:
$account->name = $data['name']; $account->name = $data['name'];
$account->active = $data['active']; $account->active = $data['active'];
$account->virtual_balance = trim($data['virtualBalance']) === '' ? '0' : $data['virtualBalance']; $account->virtual_balance = '' === trim($data['virtualBalance']) ? '0' : $data['virtualBalance'];
$account->iban = $data['iban']; $account->iban = $data['iban'];
$account->save(); $account->save();
if (isset($data['currency_id']) && $data['currency_id'] === 0) { if (isset($data['currency_id']) && 0 === $data['currency_id']) {
unset($data['currency_id']); unset($data['currency_id']);
} }

View File

@@ -44,6 +44,7 @@ class JournalUpdateService
* @param array $data * @param array $data
* *
* @return TransactionJournal * @return TransactionJournal
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function update(TransactionJournal $journal, array $data): TransactionJournal public function update(TransactionJournal $journal, array $data): TransactionJournal
{ {

View File

@@ -25,7 +25,7 @@ namespace FireflyIII\Services\Internal\Update;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Recurrence; use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceMeta;
use FireflyIII\Services\Internal\Support\RecurringTransactionTrait; use FireflyIII\Services\Internal\Support\RecurringTransactionTrait;
use FireflyIII\Services\Internal\Support\TransactionServiceTrait; use FireflyIII\Services\Internal\Support\TransactionServiceTrait;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait; use FireflyIII\Services\Internal\Support\TransactionTypeTrait;

View File

@@ -69,6 +69,7 @@ class TransactionUpdateService
* @param array $data * @param array $data
* *
* @return Transaction * @return Transaction
* @throws \FireflyIII\Exceptions\FireflyException
*/ */
public function update(Transaction $transaction, array $data): Transaction public function update(Transaction $transaction, array $data): Transaction
{ {
@@ -100,7 +101,7 @@ class TransactionUpdateService
$transaction->description = $description; $transaction->description = $description;
$transaction->amount = $amount; $transaction->amount = $amount;
$transaction->foreign_amount = null; $transaction->foreign_amount = null;
$transaction->transaction_currency_id = $currency->id; $transaction->transaction_currency_id = null === $currency ? $transaction->transaction_currency_id : $currency->id;
$transaction->account_id = $account->id; $transaction->account_id = $account->id;
$transaction->reconciled = $data['reconciled']; $transaction->reconciled = $data['reconciled'];
$transaction->save(); $transaction->save();
@@ -108,11 +109,11 @@ class TransactionUpdateService
// set foreign currency // set foreign currency
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']); $foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
// set foreign amount: // set foreign amount:
if (null !== $data['foreign_amount'] && null !== $foreign) { if (null !== $foreign && null !== $data['foreign_amount']) {
$this->setForeignCurrency($transaction, $foreign); $this->setForeignCurrency($transaction, $foreign);
$this->setForeignAmount($transaction, $foreignAmount); $this->setForeignAmount($transaction, $foreignAmount);
} }
if (null === $data['foreign_amount'] || null === $foreign) { if (null === $foreign && null === $data['foreign_amount']) {
$this->setForeignCurrency($transaction, null); $this->setForeignCurrency($transaction, null);
$this->setForeignAmount($transaction, null); $this->setForeignAmount($transaction, null);
} }

View File

@@ -56,6 +56,8 @@ class PwndVerifierV2 implements Verifier
$client = new Client(); $client = new Client();
$res = $client->request('GET', $uri, $opt); $res = $client->request('GET', $uri, $opt);
} catch (GuzzleException|Exception $e) { } catch (GuzzleException|Exception $e) {
Log::error(sprintf('Could not verify password security: %s', $e->getMessage()));
return true; return true;
} }
Log::debug(sprintf('Status code returned is %d', $res->getStatusCode())); Log::debug(sprintf('Status code returned is %d', $res->getStatusCode()));
@@ -63,7 +65,7 @@ class PwndVerifierV2 implements Verifier
return true; return true;
} }
$strpos = stripos($res->getBody()->getContents(), $rest); $strpos = stripos($res->getBody()->getContents(), $rest);
if ($strpos === false) { if (false === $strpos) {
Log::debug(sprintf('%s was not found in result body. Return true.', $rest)); Log::debug(sprintf('%s was not found in result body. Return true.', $rest));
return true; return true;

View File

@@ -42,17 +42,17 @@ class Attempt extends SpectreObject
/** @var Carbon */ /** @var Carbon */
private $consentGivenAt; private $consentGivenAt;
/** @var array */ /** @var array */
private $consentTypes = []; private $consentTypes;
/** @var Carbon */ /** @var Carbon */
private $createdAt; private $createdAt;
/** @var array */ /** @var array */
private $customFields = []; private $customFields;
/** @var bool */ /** @var bool */
private $dailyRefresh; private $dailyRefresh;
/** @var string */ /** @var string */
private $deviceType; private $deviceType;
/** @var array */ /** @var array */
private $excludeAccounts = []; private $excludeAccounts;
/** @var Carbon */ /** @var Carbon */
private $failAt; private $failAt;
/** @var string */ /** @var string */
@@ -60,7 +60,7 @@ class Attempt extends SpectreObject
/** @var string */ /** @var string */
private $failMessage; private $failMessage;
/** @var array */ /** @var array */
private $fetchScopes = []; private $fetchScopes;
/** @var bool */ /** @var bool */
private $finished; private $finished;
/** @var bool */ /** @var bool */
@@ -80,7 +80,7 @@ class Attempt extends SpectreObject
/** @var bool */ /** @var bool */
private $showConsentInformation; private $showConsentInformation;
/** @var array */ /** @var array */
private $stages = []; private $stages;
/** @var bool */ /** @var bool */
private $storeCredentials; private $storeCredentials;
/** @var Carbon */ /** @var Carbon */

View File

@@ -121,7 +121,7 @@ class Amount
$locale = explode(',', (string)trans('config.locale')); $locale = explode(',', (string)trans('config.locale'));
$locale = array_map('trim', $locale); $locale = array_map('trim', $locale);
setlocale(LC_MONETARY, $locale); setlocale(LC_MONETARY, $locale);
$float = round($amount, 12); $float = round($amount, $format->decimal_places);
$info = localeconv(); $info = localeconv();
$formatted = number_format($float, (int)$format->decimal_places, $info['mon_decimal_point'], $info['mon_thousands_sep']); $formatted = number_format($float, (int)$format->decimal_places, $info['mon_decimal_point'], $info['mon_thousands_sep']);