Merge pull request #399 from Block8/dc/error-handling

Better error handling for the front-end UI
This commit is contained in:
Dan Cryer 2014-05-09 15:36:50 +01:00
commit a6d1afd3f4
8 changed files with 106 additions and 39 deletions

View file

@ -10,6 +10,7 @@
namespace PHPCI;
use b8;
use b8\Exception\HttpException;
use b8\Http\Response;
use b8\Http\Response\RedirectResponse;
use b8\View;
@ -70,7 +71,25 @@ class Application extends b8\Application
*/
public function handleRequest()
{
$this->response = parent::handleRequest();
try {
$this->response = parent::handleRequest();
} catch (HttpException $ex) {
$this->config->set('page_title', 'Error');
$view = new View('exception');
$view->exception = $ex;
$this->response->setResponseCode($ex->getErrorCode());
$this->response->setContent($view->render());
} catch (\Exception $ex) {
$this->config->set('page_title', 'Error');
$view = new View('exception');
$view->exception = $ex;
$this->response->setResponseCode(500);
$this->response->setContent($view->render());
}
if (View::exists('layout') && $this->response->hasLayout()) {
$view = new View('layout');

View file

@ -21,11 +21,16 @@ class BuildFactory
/**
* @param $buildId
* @return Build
* @throws \Exception
*/
public static function getBuildById($buildId)
{
$build = Factory::getStore('Build')->getById($buildId);
if (empty($build)) {
throw new \Exception('Build ID ' . $buildId . ' does not exist.');
}
return self::getBuild($build);
}

View file

@ -10,6 +10,7 @@
namespace PHPCI\Controller;
use b8;
use b8\Exception\HttpException\NotFoundException;
use PHPCI\BuildFactory;
use PHPCI\Model\Build;
@ -36,7 +37,16 @@ class BuildController extends \PHPCI\Controller
*/
public function view($buildId)
{
$build = BuildFactory::getBuildById($buildId);
try {
$build = BuildFactory::getBuildById($buildId);
} catch (\Exception $ex) {
$build = null;
}
if (empty($build)) {
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
}
$this->view->plugins = $this->getUiPlugins();
$this->view->build = $build;
$this->view->data = $this->getBuildData($build);
@ -110,6 +120,10 @@ class BuildController extends \PHPCI\Controller
{
$copy = BuildFactory::getBuildById($buildId);
if (empty($copy)) {
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
}
$build = new Build();
$build->setProjectId($copy->getProjectId());
$build->setCommitId($copy->getCommitId());
@ -134,11 +148,10 @@ class BuildController extends \PHPCI\Controller
throw new \Exception('You do not have permission to do that.');
}
$build = BuildFactory::getBuildById($buildId);
$build = BuildFactory::getBuildById($buildId);
if (!$build) {
$this->response->setResponseCode(404);
return '404 - Not Found';
if (empty($build)) {
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
}
$this->buildStore->delete($build);

View file

@ -65,7 +65,8 @@ class BuildStatusController extends \PHPCI\Controller
public function view($projectId)
{
$project = $this->projectStore->getById($projectId);
if (!$project) {
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
}

View file

@ -9,17 +9,17 @@
namespace PHPCI\Controller;
use b8;
use b8\Controller;
use b8\Form;
use b8\Exception\HttpException\ForbiddenException;
use b8\Exception\HttpException\NotFoundException;
use b8\Store;
use PHPCI\BuildFactory;
use PHPCI\Helper\Github;
use PHPCI\Helper\SshKey;
use PHPCI\Model\Build;
use PHPCI\Model\Project;
use b8;
use b8\Config;
use b8\Controller;
use b8\Store;
use b8\Form;
use b8\Exception\HttpException\NotFoundException;
/**
* Project Controller - Allows users to create, edit and view projects.
@ -41,8 +41,8 @@ class ProjectController extends \PHPCI\Controller
public function init()
{
$this->buildStore = Store\Factory::getStore('Build');
$this->projectStore = Store\Factory::getStore('Project');
$this->buildStore = Store\Factory::getStore('Build');
$this->projectStore = Store\Factory::getStore('Project');
}
/**
@ -51,17 +51,18 @@ class ProjectController extends \PHPCI\Controller
public function view($projectId)
{
$project = $this->projectStore->getById($projectId);
if (!$project) {
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
}
$page = $this->getParam('p', 1);
$builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * 10));
$page = $this->getParam('p', 1);
$builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * 10));
$this->view->builds = $builds[0];
$this->view->total = $builds[1];
$this->view->project = $project;
$this->view->page = $page;
$this->view->builds = $builds[0];
$this->view->total = $builds[1];
$this->view->project = $project;
$this->view->page = $page;
$this->config->set('page_title', $project->getTitle());
@ -76,6 +77,10 @@ class ProjectController extends \PHPCI\Controller
/* @var \PHPCI\Model\Project $project */
$project = $this->projectStore->getById($projectId);
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
}
$build = new Build();
$build->setProjectId($projectId);
$build->setCommitId('Manual');
@ -96,7 +101,7 @@ class ProjectController extends \PHPCI\Controller
public function delete($projectId)
{
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
throw new ForbiddenException('You do not have permission to do that.');
}
$project = $this->projectStore->getById($projectId);
@ -120,10 +125,10 @@ class ProjectController extends \PHPCI\Controller
*/
protected function getLatestBuildsHtml($projectId, $start = 0)
{
$criteria = array('project_id' => $projectId);
$order = array('id' => 'DESC');
$builds = $this->buildStore->getWhere($criteria, 10, $start, array(), $order);
$view = new b8\View('BuildsTable');
$criteria = array('project_id' => $projectId);
$order = array('id' => 'DESC');
$builds = $this->buildStore->getWhere($criteria, 10, $start, array(), $order);
$view = new b8\View('BuildsTable');
foreach ($builds['items'] as &$build) {
$build = BuildFactory::getBuild($build);
@ -142,7 +147,7 @@ class ProjectController extends \PHPCI\Controller
$this->config->set('page_title', 'Add Project');
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
throw new ForbiddenException('You do not have permission to do that.');
}
$method = $this->request->getMethod();
@ -210,11 +215,15 @@ class ProjectController extends \PHPCI\Controller
public function edit($projectId)
{
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
throw new ForbiddenException('You do not have permission to do that.');
}
$method = $this->request->getMethod();
$project = $this->projectStore->getById($projectId);
$method = $this->request->getMethod();
$project = $this->projectStore->getById($projectId);
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
}
$this->config->set('page_title', 'Edit: ' . $project->getTitle());
@ -234,7 +243,7 @@ class ProjectController extends \PHPCI\Controller
}
$form = $this->projectForm($values, 'edit/' . $projectId);
$form = $this->projectForm($values, 'edit/' . $projectId);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('ProjectForm');

View file

@ -10,6 +10,8 @@
namespace PHPCI\Controller;
use b8;
use b8\Exception\HttpException\ForbiddenException;
use b8\Exception\HttpException\NotFoundException;
use b8\Form;
use PHPCI\Controller;
use PHPCI\Model\User;
@ -106,12 +108,11 @@ class UserController extends Controller
public function add()
{
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
throw new ForbiddenException('You do not have permission to do that.');
}
$this->config->set('page_title', 'Add User');
$method = $this->request->getMethod();
if ($method == 'POST') {
@ -150,14 +151,17 @@ class UserController extends Controller
public function edit($userId)
{
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
throw new ForbiddenException('You do not have permission to do that.');
}
$method = $this->request->getMethod();
$user = $this->userStore->getById($userId);
$this->config->set('page_title', 'Edit: ' . $user->getName());
if (empty($user)) {
throw new NotFoundException('User with ID: ' . $userId . ' does not exist.');
}
$this->config->set('page_title', 'Edit: ' . $user->getName());
if ($method == 'POST') {
$values = $this->getParams();
@ -244,10 +248,15 @@ class UserController extends Controller
public function delete($userId)
{
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
throw new ForbiddenException('You do not have permission to do that.');
}
$user = $this->userStore->getById($userId);
if (empty($user)) {
throw new NotFoundException('User with ID: ' . $userId . ' does not exist.');
}
$this->userStore->delete($user);
header('Location: '.PHPCI_URL.'user');

View file

@ -0,0 +1,9 @@
<div class="panel panel-danger">
<div class="panel-heading">
<h2 class="panel-title">Sorry, there was a problem</h2>
</div>
<div class="panel-body">
<?php print $exception->getMessage(); ?>
</div>
</div>

View file

@ -59,8 +59,10 @@ if (!file_exists(dirname(__FILE__) . '/vendor/autoload.php') && defined('PHPCI_I
// Load Composer autoloader:
require_once(dirname(__FILE__) . '/vendor/autoload.php');
$loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
Handler::register($loggerConfig->getFor('_'));
if (defined('PHPCI_IS_CONSOLE') && PHPCI_IS_CONSOLE) {
$loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
Handler::register($loggerConfig->getFor('_'));
}
// Load configuration if present:
$conf = array();