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) {
function getResponse() {
return $this->response;
}
+
+ static function cleanBuffers() {
+ while (ob_get_level()) {
+ if (!@ob_end_clean()) {
+ break;
+ }
+ }
+ }
}
<?php $this->layout("layout") ?>
<div class="page-header">
- <h1>404 <small>Not Found</small>
+ <h1>Not Found <small>404</small></h1>
</div>
+<p>
+ Page <?= $this->e($request->getRequestUrl()) ?> was not found.
+</p>
<?php $this->layout("layout") ?>
<div class="page-header">
- <h1>405 <small>Method Not Allowed</small>
+ <h1>Method Not Allowed <small>405</small>
</div>
+<p>
+ Request method <?= $this->e($request->getRequestMethod()) ?> is not allowed.
+</p>
--- /dev/null
+<?php $this->layout("layout") ?>
+
+<div class="page-header">
+ <h1>Application error <small>500</small></h1>
+</div>
+
+<?= $this->fetch("alert", compact("exception")) ?>
<div class="alert alert-danger" role="alert">
- <strong><?= $this->e($exception->getMessage()) ?></strong><br>
+ <strong><?= nl2br($this->e($exception->getMessage())) ?></strong><br>
<?php if ($exception instanceof \app\Github\Exception\TokenException) : ?>
You might want to try to <a href="<?= $baseUrl->mod("./github/signin") ?>">renew your token</a>!
<?php endif; ?>
You might want to try to <a href="<?= $baseUrl->mod("./github/signin") ?>">try again</a>!
<?php endif; ?>
</div>
+
+<?php if (APP_ENVIRONMENT != "production") : ?>
+<h3>Stack Trace</h3>
+<pre><?= $this->e($exception->getTraceAsString()) ?></pre>
+<?php endif; ?>
<?= $this->e($v->tag->name) ?>
<span class="label label-default pull-right">Tag</span>
<?php else : ?>
- <?= $this->e($v->release->name) ?>
+ <?= $this->e($v->release->name ?: $v->tag->name) ?>
<span class="label label-info pull-right">Release</span>
<?php endif; ?>
</h3>
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();
+}
+?>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <title>Application Error</title>
+ <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
+ </head>
+ <body>
+ <div class="container">
+ <div class="jumbotron">
+ <h1>Application Error</h1>
+ <h2>Aww, you really gotta do that?!</h2>
+ <p class="text-danger">
+ <strong><?= htmlspecialchars($error) ?></strong>
+ </p>
+ <p>
+ Sorry, anyway.
+ </p>
+ </div>
+ <?php if (APP_ENVIRONMENT != "production") : ?>
+ <pre><?= htmlspecialchars($stack) ?></pre>
+ <?php endif; ?>
+ </div>
+ </body>
+</html>