Skip to content

Commit 5300cdd

Browse files
committed
Globally handle exception and show error
1 parent 167072b commit 5300cdd

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

core/Application.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public function logout()
6969

7070
public function run()
7171
{
72-
echo $this->router->resolve();
72+
try {
73+
echo $this->router->resolve();
74+
} catch (\Exception $e) {
75+
echo $this->router->renderView('_error', [
76+
'exception' => $e,
77+
]);
78+
}
7379
}
7480
}

core/Router.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ public function resolve()
4949
return $this->renderView($callback);
5050
}
5151
if (is_array($callback)) {
52+
/**
53+
* @var $controller \app\core\Controller
54+
*/
5255
$controller = new $callback[0];
5356
Application::$app->controller = $controller;
57+
$middlewares = $controller->getMiddlewares();
58+
foreach ($middlewares as $middleware) {
59+
$middleware->execute();
60+
}
5461
$callback[0] = $controller;
5562
}
5663
return call_user_func($callback, $this->request, $this->response);

views/_error.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 7/11/2020
5+
* Time: 10:12 AM
6+
*/
7+
/** @var $exception \Exception */
8+
?>
9+
10+
<h3><?php echo $exception->getCode() ?> - <?php echo $exception->getMessage() ?></h3>

0 commit comments

Comments
 (0)