From b0a2ea11d6b820abe4758aaf56e6d226ded72942 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 8 May 2015 21:15:17 +0200 Subject: [PATCH] nicer exception handling --- app/Web.php | 32 +++++++++++++++++++++++-------- app/views/404.phtml | 5 ++++- app/views/405.phtml | 5 ++++- app/views/500.phtml | 7 +++++++ app/views/alert.phtml | 7 ++++++- app/views/github/repo.phtml | 2 +- public/index.php | 38 ++++++++++++++++++++++++++++++++++--- 7 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 app/views/500.phtml diff --git a/app/Web.php b/app/Web.php index c9659b6..0353a99 100644 --- a/app/Web.php +++ b/app/Web.php @@ -29,28 +29,36 @@ class Web switch ($route[0]) { case Dispatcher::NOT_FOUND: - $this->response->setResponseCode(404); - $this->response->getBody()->append($this->view->render("404")); + $this->display(404, null, 404); break; case Dispatcher::METHOD_NOT_ALLOWED: - $this->response->setResponseCode(405); - $this->response->setHeader("Allowed", $route[1]); - $this->response->getBody()->append($this->view->render("405")); + $this->display(405, null, 405, ["Allowed" => $route[1]]); break; case Dispatcher::FOUND: list(, $handler, $args) = $route; - $handler(array_map("urldecode", $args)); + try { + $handler(array_map("urldecode", $args)); + } catch (\Exception $exception) { + self::cleanBuffers(); + $this->display(500, compact("exception"), 500, ["X-Exception", get_class($exception)]); + } break; } $this->response->send(); } - function display($view, array $data = []) { + function display($view, array $data = null, $code = null, array $headers = []) { + if (isset($code)) { + $this->response->setResponseCode($code); + } + if ($headers) { + $this->response->addHeaders($headers); + } $this->response->getBody()->append( - $this->view->render($view, $data)); + $this->view->render($view, (array) $data)); } function redirect($url, $code = 302) { @@ -73,4 +81,12 @@ class Web function getResponse() { return $this->response; } + + static function cleanBuffers() { + while (ob_get_level()) { + if (!@ob_end_clean()) { + break; + } + } + } } diff --git a/app/views/404.phtml b/app/views/404.phtml index 491df9c..f8fd0bb 100644 --- a/app/views/404.phtml +++ b/app/views/404.phtml @@ -1,5 +1,8 @@ layout("layout") ?> +

+ Page e($request->getRequestUrl()) ?> was not found. +

diff --git a/app/views/405.phtml b/app/views/405.phtml index ec25164..0143f67 100644 --- a/app/views/405.phtml +++ b/app/views/405.phtml @@ -1,5 +1,8 @@ layout("layout") ?> +

+ Request method e($request->getRequestMethod()) ?> is not allowed. +

diff --git a/app/views/500.phtml b/app/views/500.phtml new file mode 100644 index 0000000..98692b9 --- /dev/null +++ b/app/views/500.phtml @@ -0,0 +1,7 @@ +layout("layout") ?> + + + +fetch("alert", compact("exception")) ?> diff --git a/app/views/alert.phtml b/app/views/alert.phtml index bd8c0b7..5d0a516 100644 --- a/app/views/alert.phtml +++ b/app/views/alert.phtml @@ -1,6 +1,6 @@ + + +

Stack Trace

+
e($exception->getTraceAsString()) ?>
+ diff --git a/app/views/github/repo.phtml b/app/views/github/repo.phtml index 36849c0..d321830 100644 --- a/app/views/github/repo.phtml +++ b/app/views/github/repo.phtml @@ -181,7 +181,7 @@ e($v->tag->name) ?> Tag - e($v->release->name) ?> + e($v->release->name ?: $v->tag->name) ?> Release diff --git a/public/index.php b/public/index.php index 5718129..7603f62 100644 --- a/public/index.php +++ b/public/index.php @@ -2,6 +2,38 @@ namespace app; -$bootstrap = require "../app/bootstrap.php"; -$injector = $bootstrap(["config", "github", "plates", "model", "web"]); -$injector->execute(Web::class); +try { + $bootstrap = require "../app/bootstrap.php"; + $injector = $bootstrap(["config", "github", "plates", "model", "web"]); + return $injector->execute(Web::class); +} catch (\Exception $e) { + $error = $e->getMessage(); + $stack = $e->getTraceAsString(); + @header("X-Exception: ".get_class($e), false, 500); + Web::cleanBuffers(); +} +?> + + + + Application Error + + + +
+
+

Application Error

+

Aww, you really gotta do that?!

+

+ +

+

+ Sorry, anyway. +

+
+ +
+ +
+ + -- 2.30.2