Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Roach committed Jun 2, 2018
1 parent 28dbc60 commit 907b27b
Show file tree
Hide file tree
Showing 17 changed files with 575 additions and 66 deletions.
33 changes: 33 additions & 0 deletions app/Exceptions/FamilyAccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
* Exception thrown when a family cannot be accessed due to privacy rules.
*/
class FamilyAccessDeniedException extends AccessDeniedHttpException {
/**
* FamilyNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This family does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/FamilyNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Exception thrown when a family does not exist.
*/
class FamilyNotFoundException extends NotFoundHttpException {
/**
* FamilyNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This family does not exist or you do not have permission to view it.'));
}
}
65 changes: 65 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Exception;
use Fisharebest\Webtrees\Http\Controllers\ErrorController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;

/**
* Convert an exception into an HTTP response
*/
class Handler {
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Exception $exception
*
* @return Response
*/
public function render(Request $request, Exception $exception): Response {
if ($exception instanceof HttpException) {
// Show a friendly page for expected exceptions.
if ($request->isXmlHttpRequest()) {
$response = new Response($exception->getMessage(), $exception->getStatusCode());
} else {
$controller = new ErrorController;
$response = $controller->errorResponse($exception);
}
} else {
// Show an error page for unexpected exceptions.
if (getenv('DEBUG')) {
// Local dev environment? Show full debug.
$whoops = new Run;
$whoops->pushHandler(new PrettyPageHandler);
$whoops->handleException($exception);
} else {
// Running remotely? Show a friendly error page.
$controller = new ErrorController;
$response = $controller->unhandledExceptionResponse($request, $exception);
}
}

return $response;
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/IndividualAccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
* Exception thrown when an individual cannot be accessed due to privacy rules.
*/
class IndividualAccessDeniedException extends AccessDeniedHttpException {
/**
* IndividualNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This individual does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/IndividualNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Exception thrown when an individual does not exist.
*/
class IndividualNotFoundException extends NotFoundHttpException {
/**
* IndividualNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This individual does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/MediaAccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
* Exception thrown when a media cannot be accessed due to privacy rules.
*/
class MediaAccessDeniedException extends AccessDeniedHttpException {
/**
* MediaNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This media object does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/MediaNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Exception thrown when a media does not exist.
*/
class MediaNotFoundException extends NotFoundHttpException {
/**
* MediaNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This media object does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/NoteAccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
* Exception thrown when a note cannot be accessed due to privacy rules.
*/
class NoteAccessDeniedException extends AccessDeniedHttpException {
/**
* NoteNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This note does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/NoteNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Exception thrown when a note does not exist.
*/
class NoteNotFoundException extends NotFoundHttpException {
/**
* NoteNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This note does not exist or you do not have permission to view it.'));
}
}
33 changes: 33 additions & 0 deletions app/Exceptions/RecordAccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace Fisharebest\Webtrees\Exceptions;

use Fisharebest\Webtrees\I18N;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
* Exception thrown when a record cannot be accessed due to privacy rules.
*/
class RecordAccessDeniedException extends AccessDeniedHttpException {
/**
* RecordNotFoundException constructor.
*/
public function __construct() {
parent::__construct(I18N::translate('This record does not exist or you do not have permission to view it.'));
}
}
Loading

0 comments on commit 907b27b

Please sign in to comment.