Skip to content

Commit

Permalink
Merge pull request #152 from Divyesh000/404
Browse files Browse the repository at this point in the history
create a dynamic error controller
  • Loading branch information
creme332 authored May 8, 2024
2 parents 8464cfe + a313b70 commit 831b4e9
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/controllers/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/controllers/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Steamy\Controller;

use Steamy\Core\Controller;

class Error
{
use Controller;

private array $view_data;

public function __construct()
{
$this->view_data['error_message'] = '500 - Internal server error';
$this->view_data['extended_error_message'] = 'Something bad happened';
}

public function handleDatabaseError(): void
{
$this->view_data['extended_error_message'] = 'Unable to connect to database.';

$this->view(
'Error',
$this->view_data,
template_title: "Error",
enableIndexing: false
);
}

public function handlePageNotFoundError($extended_error_message = 'Ensure that you have properly typed the URL.'
): void {
$this->view_data['error_message'] = '404 - Page not found';
$this->view_data['extended_error_message'] = $extended_error_message;

$this->view(
'Error',
$this->view_data,
template_title: "Page not found",
enableIndexing: false
);
}

public function handleUnknownError(): void
{
$this->view_data['extended_error_message'] = 'The server has encountered a situation
it does not know how to handle.';

$this->view(
'Error',
$this->view_data,
template_title: "Unknown error",
enableIndexing: false
);
}

public function index(): void
{
$this->handlePageNotFoundError();
}
}
2 changes: 1 addition & 1 deletion src/controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function index(): void
}

// if url follows some other format display error page
(new _404())->index();
(new Error())->handlePageNotFoundError();
}
}

4 changes: 2 additions & 2 deletions src/controllers/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError('Product does not exist');
die();
}
}
Expand All @@ -254,7 +254,7 @@ public function index(): void

// if product was not found, display error page
if (empty($this->product)) {
(new _404())->index();
(new Error())->handlePageNotFoundError('Product does not exist.');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ private function handleFormSubmission(): void
Utility::redirect('login');
}

// TODO: redirect to some error page
Utility::redirect('home');
(new Error())->index("An error occurred while processing your registration. Please try again later.");
die();
} else {
$this->loadDataToForm($form_data);
}
Expand Down Expand Up @@ -148,7 +148,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->index("Page not found");
die();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function index(): void
// check if URL is not /shop
if (Utility::getURL() !== "shop") {
// let 404 controller handle this
(new _404())->index();
(new Error())->handlePageNotFoundError();
return;
}

Expand Down
24 changes: 0 additions & 24 deletions src/controllers/_404.php

This file was deleted.

22 changes: 19 additions & 3 deletions src/core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

namespace Steamy\Core;

use Steamy\Controller\_404;
use Steamy\Controller\Error;
use Steamy\Controller\API;
use Throwable;

class App
{

/**
* Global exception handler
* @param Throwable $exception
* @return void
*/
public function exception_handler(Throwable $exception): void
{
// echo "Uncaught exception: ", $exception->getMessage(), "\n";
(new Error())->handleUnknownError();
}


/**
* Calls appropriate controller class to deal with URL.
* @return void
*/
public function loadController(): void
{
set_exception_handler(array($this, "exception_handler"));

$URL = Utility::splitURL();

switch ($URL[0]) {
Expand All @@ -28,8 +44,8 @@ public function loadController(): void
// call appropriate controller
(new $controllerClassName())->index();
} else {
// Fallback to 404 controller
(new _404())->index();
// Display error page
(new Error())->handlePageNotFoundError();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function view(
if (file_exists($view_file_path)) {
include $view_file_path;
} else {
include __DIR__ . '/../views/404.php';
include __DIR__ . '/../views/Error.php';
}
$template_content = ob_get_contents();
ob_end_clean();
Expand Down
4 changes: 2 additions & 2 deletions src/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PDO;
use PDOException;
use stdClass;
use Steamy\Controller\Error;

trait Database
{
Expand All @@ -25,8 +26,7 @@ protected static function connect(): PDO
} catch (PDOException $e) {
// if PHPUnit is not running, handle the exception
if (!defined('PHPUNIT_STEAMY_TESTSUITE')) {
// TODO: Display a user-friendly error message
Utility::show("Sorry, we're unable to process your request at the moment. Please try again later.");
(new Error())->handleDatabaseError();
die();
} else {
// if PHPUnit is running, re-throw the exception to allow it to propagate
Expand Down
4 changes: 0 additions & 4 deletions src/views/404.php

This file was deleted.

Loading

0 comments on commit 831b4e9

Please sign in to comment.