diff --git a/src/Controllers/AbstractController.php b/src/Controllers/AbstractController.php index 9235ff4d..8682edb4 100644 --- a/src/Controllers/AbstractController.php +++ b/src/Controllers/AbstractController.php @@ -38,6 +38,21 @@ class AbstractController } + protected function interceptErrors($action) + { + try + { + $action(); + return true; + } + catch (SimpleException $e) + { + \Chibi\Util\Headers::setCode(400); + Messenger::fail($e->getMessage()); + return false; + } + } + protected function redirectToLastVisitedUrl($filter = null) { $targetUrl = SessionHelper::getLastVisitedUrl($filter); diff --git a/src/Controllers/ApiController.php b/src/Controllers/ApiController.php index 09cc1caf..c008007d 100644 --- a/src/Controllers/ApiController.php +++ b/src/Controllers/ApiController.php @@ -3,10 +3,10 @@ class ApiController extends AbstractController { public function runAction() { - $context = Core::getContext(); - - try + $this->interceptErrors(function() { + $context = Core::getContext(); + if (!Auth::isLoggedIn()) { $auth = InputHelper::get('auth'); @@ -36,12 +36,7 @@ class ApiController extends AbstractController } $context->transport->status = Api::run($job, $jobArgs); - } - catch (Exception $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); $this->renderAjax(); } diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index de18e38d..a08feee9 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -11,21 +11,18 @@ class AuthController extends AbstractController public function loginAction() { - try + $success = $this->interceptErrors(function() { $suppliedName = InputHelper::get('name'); $suppliedPassword = InputHelper::get('password'); $remember = boolval(InputHelper::get('remember')); Auth::login($suppliedName, $suppliedPassword, $remember); - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - $this->renderView('auth-login'); - } + }); - $this->redirectToLastVisitedUrl('auth'); + if ($success) + $this->redirectToLastVisitedUrl('auth'); + else + $this->renderView('auth-login'); } public function logoutAction() diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 387b7216..8849c76a 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -7,7 +7,7 @@ class PostController extends AbstractController $context->source = $source; $context->additionalInfo = $additionalInfo; - try + $this->interceptErrors(function() use ($context, $query, $page, $source, $additionalInfo) { $query = trim($query); $context->transport->searchQuery = $query; @@ -31,12 +31,7 @@ class PostController extends AbstractController $context->transport->posts = $ret->entities; $context->transport->paginator = $ret; - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); $this->renderView('post-list-wrapper'); } diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 41b33f6f..153cbe6f 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -80,7 +80,7 @@ class TagController extends AbstractController public function mergeAction() { - try + $this->interceptErrors(function() { Api::run( new MergeTagsJob(), @@ -90,12 +90,7 @@ class TagController extends AbstractController ]); Messenger::success('Tags merged successfully.'); - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); if ($this->isAjax()) $this->renderAjax(); @@ -110,7 +105,7 @@ class TagController extends AbstractController public function renameAction() { - try + $this->interceptErrors(function() { Api::run( new RenameTagsJob(), @@ -120,12 +115,7 @@ class TagController extends AbstractController ]); Messenger::success('Tag renamed successfully.'); - } - catch (Exception $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); if ($this->isAjax()) $this->renderAjax(); diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 767b3ff4..24db3cbc 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -27,7 +27,7 @@ class UserController extends AbstractController { $this->prepareGenericView($identifier, 'settings'); - try + $this->interceptErrors(function() use ($identifier) { $suppliedSafety = InputHelper::get('safety'); $desiredSafety = PostSafety::makeFlags($suppliedSafety); @@ -50,12 +50,7 @@ class UserController extends AbstractController Auth::setCurrentUser($user); Messenger::success('Browsing settings updated!'); - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); if ($this->isAjax()) $this->renderAjax(); @@ -67,7 +62,7 @@ class UserController extends AbstractController { $this->prepareGenericView($identifier, 'edit'); - try + $this->interceptErrors(function() use ($identifier) { $this->requirePasswordConfirmation(); @@ -107,12 +102,7 @@ class UserController extends AbstractController $message .= ' You will be sent an e-mail address confirmation message soon.'; Messenger::success($message); - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); if ($this->isAjax()) $this->renderAjax(); @@ -124,7 +114,7 @@ class UserController extends AbstractController { $this->prepareGenericView($identifier, 'delete'); - try + $success = $this->interceptErrors(function() use ($identifier) { $this->requirePasswordConfirmation(); @@ -135,19 +125,14 @@ class UserController extends AbstractController $user = UserModel::tryGetById(Auth::getCurrentUser()->getId()); if (!$user) Auth::logOut(); + }); + if ($success) $this->redirectToMainPage(); - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - - if ($this->isAjax()) - $this->renderAjax(); - else - $this->renderView('user-view'); - } + elseif ($this->isAjax()) + $this->renderAjax(); + else + $this->renderView('user-view'); } public function toggleSafetyAction($safety) @@ -215,7 +200,7 @@ class UserController extends AbstractController public function registrationAction() { - try + $this->interceptErrors(function() { if (InputHelper::get('password1') != InputHelper::get('password2')) throw new SimpleException('Specified passwords must be the same'); @@ -243,12 +228,7 @@ class UserController extends AbstractController $message .= ' Your registration must be now confirmed by staff.'; Messenger::success($message); - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); $this->renderView('user-registration'); } @@ -264,7 +244,7 @@ class UserController extends AbstractController $this->assets->setSubTitle('account activation'); $identifier = InputHelper::get('identifier'); - try + $this->interceptErrors(function() use ($tokenText, $identifier) { if (empty($tokenText)) { @@ -287,12 +267,7 @@ class UserController extends AbstractController if (!Core::getConfig()->registration->staffActivation) Auth::setCurrentUser($user); } - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); $this->renderView('message'); } @@ -308,7 +283,7 @@ class UserController extends AbstractController $this->assets->setSubTitle('password reset'); $identifier = InputHelper::get('identifier'); - try + $this->interceptErrors(function() use ($tokenText, $identifier) { if (empty($tokenText)) { @@ -328,12 +303,7 @@ class UserController extends AbstractController Auth::setCurrentUser($ret->user); } - } - catch (SimpleException $e) - { - \Chibi\Util\Headers::setCode(400); - Messenger::fail($e->getMessage()); - } + }); $this->renderView('message'); }