Skip to content

Firefly III: a personal finances manager

Throw errors instead of abort()

James Cole1a110de5979c
diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php
index 7bf7a47d82..93376784cd 100644
--- a/app/Http/Controllers/Auth/AuthController.php
+++ b/app/Http/Controllers/Auth/AuthController.php
@@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Auth;
use Auth;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Role;
use FireflyIII\User;
@@ -151,7 +152,7 @@ class AuthController extends Controller
return redirect($this->redirectPath());
}
- abort(500, 'Not a user!');
+ throw new FireflyException('The authenticated user object is invalid.');
return redirect($this->redirectPath());
diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php
index 78fbc0ec4f..8e11142d10 100644
--- a/app/Http/Controllers/JsonController.php
+++ b/app/Http/Controllers/JsonController.php
@@ -3,6 +3,7 @@
use Amount;
use Carbon\Carbon;
use Config;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
@@ -247,7 +248,7 @@ class JsonController extends Controller
{
$pref = Preferences::get('tour', true);
if (!$pref) {
- abort(404);
+ throw new FireflyException('Cannot find preference for tour. Exit.');
}
$headers = ['main-content', 'sidebar-toggle', 'account-menu', 'budget-menu', 'report-menu', 'transaction-menu', 'option-menu', 'main-content-end'];
$steps = [];
diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php
index ee9fdf0788..060a41dbb8 100644
--- a/app/Http/Requests/JournalFormRequest.php
+++ b/app/Http/Requests/JournalFormRequest.php
@@ -6,6 +6,7 @@ namespace FireflyIII\Http\Requests;
use Auth;
use Carbon\Carbon;
use Exception;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionType;
use Input;
@@ -32,6 +33,7 @@ class JournalFormRequest extends Request
public function getJournalData()
{
$tags = $this->get('tags') ?? '';
+
return [
'what' => $this->get('what'),
'description' => $this->get('description'),
@@ -86,7 +88,7 @@ class JournalFormRequest extends Request
$rules['category'] = 'between:1,255';
break;
default:
- abort(500, 'Cannot handle ' . $what);
+ throw new FireflyException('Cannot handle transaction type of type ' . e($what) . '.');
break;
}
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 99cf93bff8..7a90bb7386 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -7,6 +7,7 @@ use Auth;
use Carbon\Carbon;
use Config;
use DB;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
@@ -482,7 +483,9 @@ class AccountRepository implements AccountRepositoryInterface
$existingAccount = Account::firstOrNullEncrypted($searchData);
if (!$existingAccount) {
Log::error('Account create error: ' . $newAccount->getErrors()->toJson());
- abort(500);
+ throw new FireflyException(
+ 'Cannot create a new account. See also the log files. First error is: ' . e($newAccount->getErrors()->first()) . '.'
+ );
}
$newAccount = $existingAccount;
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 6184ff9d47..c11f3a4a97 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -6,6 +6,7 @@ namespace FireflyIII\Repositories\Journal;
use Auth;
use Carbon\Carbon;
use DB;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
@@ -349,14 +350,14 @@ class JournalRepository implements JournalRepositoryInterface
if (is_null($toAccount)) {
Log::error('"to"-account is null, so we cannot continue!');
- abort(500, '"to"-account is null, so we cannot continue!');
+ throw new FireflyException('"to"-account is null, so we cannot continue!');
// @codeCoverageIgnoreStart
}
// @codeCoverageIgnoreEnd
if (is_null($fromAccount)) {
Log::error('"from"-account is null, so we cannot continue!');
- abort(500, '"from"-account is null, so we cannot continue!');
+ throw new FireflyException('"from"-account is null, so we cannot continue!');
// @codeCoverageIgnoreStart
}
diff --git a/app/Rules/Actions/ActionFactory.php b/app/Rules/Actions/ActionFactory.php
index 43161b56a1..5c525e79af 100644
--- a/app/Rules/Actions/ActionFactory.php
+++ b/app/Rules/Actions/ActionFactory.php
@@ -10,6 +10,7 @@ declare(strict_types = 1);
namespace FireflyIII\Rules\Actions;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\Domain;
@@ -22,48 +23,56 @@ use FireflyIII\Support\Domain;
class ActionFactory
{
protected static $actionTypes = null;
-
+
+ /**
+ * Returns the action for the given type and journal
+ *
+ * @param RuleAction $action
+ * @param TransactionJournal $journal
+ *
+ * @return ActionInterface
+ */
+ public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface
+ {
+ $actionType = $action->action_type;
+ $class = self::getActionClass($actionType);
+
+ return new $class($action, $journal);
+ }
+
/**
* Returns the class name to be used for actions with the given name
+ *
* @param string $actionType
- * @return ActionInterface
+ *
+ * @return ActionInterface|string
+ * @throws FireflyException
*/
- public static function getActionClass(string $actionType): string {
+ public static function getActionClass(string $actionType): string
+ {
$actionTypes = self::getActionTypes();
-
+
if (!array_key_exists($actionType, $actionTypes)) {
- abort(500, 'No such action exists ("' . $actionType . '").');
+ throw new FireflyException('No such action exists ("' . e($actionType) . '").');
}
-
+
$class = $actionTypes[$actionType];
if (!class_exists($class)) {
- abort(500, 'Could not instantiate class for rule action type "' . $actionType . '" (' . $class . ').');
+ throw new FireflyException('Could not instantiate class for rule action type "' . e($actionType) . '" (' . e($class) . ').');
}
-
+
return $class;
}
-
- /**
- * Returns the action for the given type and journal
- * @param RuleAction $action
- * @param TransactionJournal $journal
- * @return ActionInterface
- */
- public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface {
- $actionType = $action->action_type;
- $class = self::getActionClass($actionType);
-
- return new $class($action, $journal);
- }
-
+
/**
* Returns a map with actiontypes, mapped to the class representing that type
*/
- protected static function getActionTypes() {
- if( !self::$actionTypes ) {
+ protected static function getActionTypes()
+ {
+ if (!self::$actionTypes) {
self::$actionTypes = Domain::getRuleActions();
}
-
+
return self::$actionTypes;
}
}
diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php
index 116670e9ff..2eaf47e354 100644
--- a/app/Rules/Triggers/TriggerFactory.php
+++ b/app/Rules/Triggers/TriggerFactory.php
@@ -10,6 +10,7 @@ declare(strict_types = 1);
namespace FireflyIII\Rules\Triggers;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\Domain;
@@ -51,13 +52,12 @@ class TriggerFactory
$triggerTypes = self::getTriggerTypes();
if (!array_key_exists($triggerType, $triggerTypes)) {
- abort(500, 'No such trigger exists ("' . $triggerType . '").');
+ throw new FireflyException('No such trigger exists ("' . e($triggerType) . '").');
}
- /** @var TriggerInterface $class */
$class = $triggerTypes[$triggerType];
if (!class_exists($class)) {
- abort(500, 'Could not instantiate class for rule trigger type "' . $triggerType . '" (' . $class . ').');
+ throw new FireflyException('Could not instantiate class for rule trigger type "' . e($triggerType) . '" (' . e($class) . ').');
}
return $class;