Simplified error intercepting in controllers

This commit is contained in:
Marcin Kurczewski 2014-05-20 22:58:20 +02:00
parent 73a64034b0
commit c9903086fb
6 changed files with 48 additions and 86 deletions

View file

@ -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);

View file

@ -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();
}

View file

@ -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()

View file

@ -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');
}

View file

@ -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();

View file

@ -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');
}