Throw errors instead of abort()

This commit is contained in:
James Cole 2016-02-17 15:52:46 +01:00
parent 317aa591c3
commit 1a110de597
7 changed files with 55 additions and 38 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Auth; namespace FireflyIII\Http\Controllers\Auth;
use Auth; use Auth;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Role; use FireflyIII\Models\Role;
use FireflyIII\User; use FireflyIII\User;
@ -151,7 +152,7 @@ class AuthController extends Controller
return redirect($this->redirectPath()); return redirect($this->redirectPath());
} }
abort(500, 'Not a user!'); throw new FireflyException('The authenticated user object is invalid.');
return redirect($this->redirectPath()); return redirect($this->redirectPath());

View File

@ -3,6 +3,7 @@
use Amount; use Amount;
use Carbon\Carbon; use Carbon\Carbon;
use Config; use Config;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Report\ReportQueryInterface; use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
@ -247,7 +248,7 @@ class JsonController extends Controller
{ {
$pref = Preferences::get('tour', true); $pref = Preferences::get('tour', true);
if (!$pref) { 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']; $headers = ['main-content', 'sidebar-toggle', 'account-menu', 'budget-menu', 'report-menu', 'transaction-menu', 'option-menu', 'main-content-end'];
$steps = []; $steps = [];

View File

@ -6,6 +6,7 @@ namespace FireflyIII\Http\Requests;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use Exception; use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use Input; use Input;
@ -32,6 +33,7 @@ class JournalFormRequest extends Request
public function getJournalData() public function getJournalData()
{ {
$tags = $this->get('tags') ?? ''; $tags = $this->get('tags') ?? '';
return [ return [
'what' => $this->get('what'), 'what' => $this->get('what'),
'description' => $this->get('description'), 'description' => $this->get('description'),
@ -86,7 +88,7 @@ class JournalFormRequest extends Request
$rules['category'] = 'between:1,255'; $rules['category'] = 'between:1,255';
break; break;
default: default:
abort(500, 'Cannot handle ' . $what); throw new FireflyException('Cannot handle transaction type of type ' . e($what) . '.');
break; break;
} }

View File

@ -7,6 +7,7 @@ use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use Config; use Config;
use DB; use DB;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
@ -482,7 +483,9 @@ class AccountRepository implements AccountRepositoryInterface
$existingAccount = Account::firstOrNullEncrypted($searchData); $existingAccount = Account::firstOrNullEncrypted($searchData);
if (!$existingAccount) { if (!$existingAccount) {
Log::error('Account create error: ' . $newAccount->getErrors()->toJson()); 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; $newAccount = $existingAccount;

View File

@ -6,6 +6,7 @@ namespace FireflyIII\Repositories\Journal;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use DB; use DB;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
@ -349,14 +350,14 @@ class JournalRepository implements JournalRepositoryInterface
if (is_null($toAccount)) { if (is_null($toAccount)) {
Log::error('"to"-account is null, so we cannot continue!'); 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 // @codeCoverageIgnoreStart
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
if (is_null($fromAccount)) { if (is_null($fromAccount)) {
Log::error('"from"-account is null, so we cannot continue!'); 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 // @codeCoverageIgnoreStart
} }

View File

@ -10,6 +10,7 @@ declare(strict_types = 1);
namespace FireflyIII\Rules\Actions; namespace FireflyIII\Rules\Actions;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\Domain; use FireflyIII\Support\Domain;
@ -23,43 +24,51 @@ class ActionFactory
{ {
protected static $actionTypes = null; protected static $actionTypes = null;
/**
* Returns the class name to be used for actions with the given name
* @param string $actionType
* @return ActionInterface
*/
public static function getActionClass(string $actionType): string {
$actionTypes = self::getActionTypes();
if (!array_key_exists($actionType, $actionTypes)) {
abort(500, 'No such action exists ("' . $actionType . '").');
}
$class = $actionTypes[$actionType];
if (!class_exists($class)) {
abort(500, 'Could not instantiate class for rule action type "' . $actionType . '" (' . $class . ').');
}
return $class;
}
/** /**
* Returns the action for the given type and journal * Returns the action for the given type and journal
*
* @param RuleAction $action * @param RuleAction $action
* @param TransactionJournal $journal * @param TransactionJournal $journal
*
* @return ActionInterface * @return ActionInterface
*/ */
public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface { public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface
{
$actionType = $action->action_type; $actionType = $action->action_type;
$class = self::getActionClass($actionType); $class = self::getActionClass($actionType);
return new $class($action, $journal); return new $class($action, $journal);
} }
/**
* Returns the class name to be used for actions with the given name
*
* @param string $actionType
*
* @return ActionInterface|string
* @throws FireflyException
*/
public static function getActionClass(string $actionType): string
{
$actionTypes = self::getActionTypes();
if (!array_key_exists($actionType, $actionTypes)) {
throw new FireflyException('No such action exists ("' . e($actionType) . '").');
}
$class = $actionTypes[$actionType];
if (!class_exists($class)) {
throw new FireflyException('Could not instantiate class for rule action type "' . e($actionType) . '" (' . e($class) . ').');
}
return $class;
}
/** /**
* Returns a map with actiontypes, mapped to the class representing that type * Returns a map with actiontypes, mapped to the class representing that type
*/ */
protected static function getActionTypes() { protected static function getActionTypes()
{
if (!self::$actionTypes) { if (!self::$actionTypes) {
self::$actionTypes = Domain::getRuleActions(); self::$actionTypes = Domain::getRuleActions();
} }

View File

@ -10,6 +10,7 @@ declare(strict_types = 1);
namespace FireflyIII\Rules\Triggers; namespace FireflyIII\Rules\Triggers;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\Domain; use FireflyIII\Support\Domain;
@ -51,13 +52,12 @@ class TriggerFactory
$triggerTypes = self::getTriggerTypes(); $triggerTypes = self::getTriggerTypes();
if (!array_key_exists($triggerType, $triggerTypes)) { 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]; $class = $triggerTypes[$triggerType];
if (!class_exists($class)) { 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; return $class;