Merge pull request #6910 from firefly-iii/fix-api-errors

Catch and verify various errors
This commit is contained in:
James Cole
2023-01-21 17:15:05 +01:00
committed by GitHub
12 changed files with 33 additions and 27 deletions

View File

@@ -110,7 +110,7 @@ abstract class Controller extends BaseController
$obj = Carbon::parse($date);
} catch (InvalidDateException|InvalidFormatException $e) {
// don't care
app('log')->warning(sprintf('Ignored invalid date "%s" in API controller parameter check: %s', $date, $e->getMessage()));
app('log')->warning(sprintf('Ignored invalid date "%s" in API controller parameter check: %s', substr($date, 0, 20), $e->getMessage()));
}
}
$bag->set($field, $obj);

View File

@@ -69,7 +69,7 @@ class DestroyController extends Controller
$this->unused = $request->boolean('unused', false);
switch ($objects) {
default:
throw new FireflyException(sprintf('This endpoint can\'t handle object "%s"', $objects));
throw new FireflyException(sprintf('200033: This endpoint can\'t handle object "%s"', $objects));
case 'budgets':
$this->destroyBudgets();
break;

View File

@@ -28,6 +28,7 @@ use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
@@ -37,6 +38,7 @@ use Illuminate\Http\JsonResponse;
class DestroyController extends Controller
{
private CurrencyRepositoryInterface $repository;
private UserRepositoryInterface $userRepository;
/**
* CurrencyRepository constructor.
@@ -49,6 +51,7 @@ class DestroyController extends Controller
$this->middleware(
function ($request, $next) {
$this->repository = app(CurrencyRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);

View File

@@ -73,7 +73,7 @@ class AttemptController extends Controller
public function index(Webhook $webhook, WebhookMessage $message): JsonResponse
{
if ($message->webhook_id !== $webhook->id) {
throw new FireflyException('Webhook and webhook message are no match');
throw new FireflyException('200040: Webhook and webhook message are no match');
}
$manager = $this->getManager();
@@ -112,10 +112,10 @@ class AttemptController extends Controller
public function show(Webhook $webhook, WebhookMessage $message, WebhookAttempt $attempt): JsonResponse
{
if ($message->webhook_id !== $webhook->id) {
throw new FireflyException('Webhook and webhook message are no match');
throw new FireflyException('200040: Webhook and webhook message are no match');
}
if ($attempt->webhook_message_id !== $message->id) {
throw new FireflyException('Webhook message and webhook attempt are no match');
throw new FireflyException('200041: Webhook message and webhook attempt are no match');
}
$manager = $this->getManager();

View File

@@ -91,10 +91,10 @@ class DestroyController extends Controller
public function destroyAttempt(Webhook $webhook, WebhookMessage $message, WebhookAttempt $attempt): JsonResponse
{
if ($message->webhook_id !== $webhook->id) {
throw new FireflyException('Webhook and webhook message are no match');
throw new FireflyException('200040: Webhook and webhook message are no match');
}
if ($attempt->webhook_message_id !== $message->id) {
throw new FireflyException('Webhook message and webhook attempt are no match');
throw new FireflyException('200041: Webhook message and webhook attempt are no match');
}
$this->repository->destroyAttempt($attempt);
@@ -119,7 +119,7 @@ class DestroyController extends Controller
public function destroyMessage(Webhook $webhook, WebhookMessage $message): JsonResponse
{
if ($message->webhook_id !== $webhook->id) {
throw new FireflyException('Webhook and webhook message are no match');
throw new FireflyException('200040: Webhook and webhook message are no match');
}
$this->repository->destroyMessage($message);
app('preferences')->mark();

View File

@@ -103,7 +103,7 @@ class MessageController extends Controller
public function show(Webhook $webhook, WebhookMessage $message): JsonResponse
{
if ($message->webhook_id !== $webhook->id) {
throw new FireflyException('Webhook and webhook message are no match');
throw new FireflyException('200040: Webhook and webhook message are no match');
}
$manager = $this->getManager();

View File

@@ -105,7 +105,7 @@ class Controller extends BaseController
$obj = Carbon::parse($date);
} catch (InvalidDateException|InvalidFormatException $e) {
// don't care
app('log')->warning(sprintf('Ignored invalid date "%s" in API v2 controller parameter check: %s', $date, $e->getMessage()));
app('log')->warning(sprintf('Ignored invalid date "%s" in API v2 controller parameter check: %s', substr($date, 0, 20), $e->getMessage()));
}
}
$bag->set($field, $obj);

View File

@@ -139,17 +139,17 @@ class FixTransactionTypes extends Command
}
);
if (0 === $collection->count()) {
throw new FireflyException(sprintf('Journal #%d has no source transaction.', $journal->id));
throw new FireflyException(sprintf('300001: Journal #%d has no source transaction.', $journal->id));
}
if (1 !== $collection->count()) {
throw new FireflyException(sprintf('Journal #%d has multiple source transactions.', $journal->id));
throw new FireflyException(sprintf('300002: Journal #%d has multiple source transactions.', $journal->id));
}
/** @var Transaction $transaction */
$transaction = $collection->first();
/** @var Account|null $account */
$account = $transaction->account;
if (null === $account) {
throw new FireflyException(sprintf('Journal #%d, transaction #%d has no source account.', $journal->id, $transaction->id));
throw new FireflyException(sprintf('300003: Journal #%d, transaction #%d has no source account.', $journal->id, $transaction->id));
}
return $account;
@@ -169,17 +169,17 @@ class FixTransactionTypes extends Command
}
);
if (0 === $collection->count()) {
throw new FireflyException(sprintf('Journal #%d has no destination transaction.', $journal->id));
throw new FireflyException(sprintf('300004: Journal #%d has no destination transaction.', $journal->id));
}
if (1 !== $collection->count()) {
throw new FireflyException(sprintf('Journal #%d has multiple destination transactions.', $journal->id));
throw new FireflyException(sprintf('300005: Journal #%d has multiple destination transactions.', $journal->id));
}
/** @var Transaction $transaction */
$transaction = $collection->first();
/** @var Account|null $account */
$account = $transaction->account;
if (null === $account) {
throw new FireflyException(sprintf('Journal #%d, transaction #%d has no destination account.', $journal->id, $transaction->id));
throw new FireflyException(sprintf('300006: Journal #%d, transaction #%d has no destination account.', $journal->id, $transaction->id));
}
return $account;

View File

@@ -249,7 +249,7 @@ class ExportData extends Command
}
}
if (0 === $final->count()) {
throw new FireflyException('Ended up with zero valid accounts to export from.');
throw new FireflyException('300007: Ended up with zero valid accounts to export from.');
}
return $final;

View File

@@ -48,7 +48,7 @@ trait VerifiesAccessToken
$repository = app(UserRepositoryInterface::class);
$user = $repository->find($userId);
if (null === $user) {
throw new FireflyException('User is unexpectedly NULL');
throw new FireflyException('300000: User is unexpectedly NULL');
}
return $user;

View File

@@ -112,7 +112,7 @@ class IsValidAttachmentModel implements Rule
TransactionJournal::class => 'validateJournal',
];
if (!array_key_exists($this->model, $methods)) {
Log::error(sprintf('Cannot validate model "%s" in %s.', $this->model, __METHOD__));
Log::error(sprintf('Cannot validate model "%s" in %s.', substr($this->model, 0, 20), __METHOD__));
return false;
}

View File

@@ -46,6 +46,9 @@ trait CurrencyValidation
$transactions = $this->getTransactionsArray($validator);
foreach ($transactions as $index => $transaction) {
if(!is_array($transaction)) {
continue;
}
// if foreign amount is present, then the currency must be as well.
if (array_key_exists('foreign_amount', $transaction)
&& !(array_key_exists('foreign_currency_id', $transaction)