added awesome friendly error page

This commit is contained in:
printempw 2016-03-12 18:37:06 +08:00
parent 706a4cb8c1
commit 752757e1fe
7 changed files with 109 additions and 17 deletions

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-02-02 21:59:06 * @Date: 2016-02-02 21:59:06
* @Last Modified by: printempw * @Last Modified by: printempw
* @Last Modified time: 2016-03-12 16:43:53 * @Last Modified time: 2016-03-12 18:15:55
*/ */
class Database class Database
@ -15,18 +15,19 @@ class Database
} }
public static function checkConfig() { public static function checkConfig() {
error_reporting(0);
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT); $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
if ($conn->connect_error) { if ($conn->connect_error) {
Utils::raise(-1, "Can not connect to mysql, check if database info correct in config.php. ". Utils::showErrorPage(-1, "Can not connect to mysql, check if database info correct in config.php. ".
$conn->connect_error); $conn->connect_error);
} }
if (!self::checkTableExist($conn)) { if (!self::checkTableExist($conn)) {
Utils::raise(-1, "Looks like that there is no `users` table in your database. ". Utils::showErrorPage(-1, "Looks like that there is no `users` table in your database. ".
"Please run `/admin/install.php` first."); "Please run `/admin/install.php` first.");
} }
$dir = dirname(dirname(__FILE__)); $dir = dirname(dirname(__FILE__));
if (!is_dir("$dir/textures/")) { if (!is_dir("$dir/textures/")) {
Utils::raise(-1, "No `textures` directory exists. Please run `/admin/install.php` ". Utils::showErrorPage(-1, "No `textures` directory exists. Please run `/admin/install.php` ".
"or put one manually."); "or put one manually.");
} }
return $conn; return $conn;

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-01-16 23:01:33 * @Date: 2016-01-16 23:01:33
* @Last Modified by: printempw * @Last Modified by: printempw
* @Last Modified time: 2016-03-06 15:27:00 * @Last Modified time: 2016-03-12 18:33:34
*/ */
class User class User
@ -114,11 +114,11 @@ class User
return Utils::fread($filename); return Utils::fread($filename);
} else { } else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
Utils::raise(-1, 'Texture no longer exists.'); Utils::showErrorPage(404, '请求的贴图已被删除。');
} }
} else { } else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
Utils::raise(-1, 'No available textures.'); Utils::showErrorPage(404, '该用户尚未上传请求的贴图类型。');
} }
} }
@ -167,7 +167,7 @@ class User
$json['skins'][$sec_model] = $this->getTexture($sec_model == "default" ? "steve" : "alex"); $json['skins'][$sec_model] = $this->getTexture($sec_model == "default" ? "steve" : "alex");
$json['cape'] = $this->getTexture('cape'); $json['cape'] = $this->getTexture('cape');
} else { } else {
Utils::raise(-1, '配置文件错误:不支持的 API_TYPE。'); Utils::showErrorPage(-1, '配置文件错误:不支持的 API_TYPE。');
} }
} else { } else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-01-16 23:01:33 * @Date: 2016-01-16 23:01:33
* @Last Modified by: printempw * @Last Modified by: printempw
* @Last Modified time: 2016-03-06 15:44:32 * @Last Modified time: 2016-03-12 18:00:02
*/ */
class Utils class Utils
@ -22,6 +22,12 @@ class Utils
die(json_encode($exception)); die(json_encode($exception));
} }
public static function showErrorPage($errno = -1, $msg = "Error occured.") {
$dir = dirname(dirname(__FILE__));
require "$dir/includes/error.tpl.php";
die();
}
/** /**
* Rename uploaded file * Rename uploaded file
* *

View File

@ -3,7 +3,7 @@
* @Author: prpr * @Author: prpr
* @Date: 2016-02-02 21:17:59 * @Date: 2016-02-02 21:17:59
* @Last Modified by: printempw * @Last Modified by: printempw
* @Last Modified time: 2016-03-12 17:20:54 * @Last Modified time: 2016-03-12 18:04:46
*/ */
function __autoload($classname) { function __autoload($classname) {
@ -12,5 +12,5 @@ function __autoload($classname) {
include_once($filename); include_once($filename);
} }
if (!file_exists($dir.'/config.php')) if (!file_exists($dir.'/config.php'))
Utils::raise(-1, '未找到 `config.php`,请确认配置文件是否存在。'); Utils::showErrorPage(-1, '未找到 `config.php`,请确认配置文件是否存在。');
require "$dir/config.php"; require "$dir/config.php";

85
includes/error.tpl.php Normal file
View File

@ -0,0 +1,85 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title>出现错误 - Blessing Skin Server</title>
</head>
<body class="container">
<p id="logo"><a href="https://github.com/printempw/blessing-skin-server" tabindex="-1">Blessing Skin Server</a></p>
<h1>出现了一些错误:</h1>
<p>错误码:<?php echo $errno; ?></p>
<p>详细信息:<?php echo $msg; ?></p>
</body>
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Ubuntu);
@import url(https://work.prinzeugen.net/font/Minecraft.css);
html {
background: #f1f1f1;
margin: 0 20px;
font-weight: 400;
}
body {
background: #FFF none repeat scroll 0% 0%;
color: #444;
font-family: Ubuntu, 'Microsoft Yahei', 'Microsoft Jhenghei', sans-serif;
margin: 140px auto 25px;
padding: 20px 20px 10px;
max-width: 700px;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.13);
}
h1, h2 {
border-bottom: 1px solid #DEDEDE;
clear: both;
color: #666;
font-size: 24px;
}
#logo a, p, h1, h2 {
font-weight: 400;
}
#logo, h1, h2 {
padding: 0px 0px 7px;
}
p {
padding-bottom: 2px;
font-size: 14px;
line-height: 1.5;
}
#logo a {
font-family: Minecraft, sans-serif;
font-size: 50px;
color: #666;
height: 84px;
line-height: 1.3em;
margin: -130px auto 25px;
padding: 0;
outline: 0;
text-decoration: none;
overflow: hidden;
display: block;
}
/* Mobile phone */
@media (max-width: 48em) {
#logo a {
font-size: 40px;
}
}
@media (max-width: 35.5em) {
#logo a {
font-size: 30px;
}
}
#logo {
margin: 6px 0 14px;
border-bottom: none;
text-align: center;
}
</style>
</html>

View File

@ -2,8 +2,8 @@
/** /**
* @Author: printempw * @Author: printempw
* @Date: 2016-01-17 13:55:20 * @Date: 2016-01-17 13:55:20
* @Last Modified by: prpr * @Last Modified by: printempw
* @Last Modified time: 2016-02-10 14:18:22 * @Last Modified time: 2016-03-12 18:03:48
*/ */
session_start(); session_start();
$dir = dirname(__FILE__); $dir = dirname(__FILE__);

View File

@ -3,7 +3,7 @@
* @Author: prpr * @Author: prpr
* @Date: 2016-01-21 13:56:40 * @Date: 2016-01-21 13:56:40
* @Last Modified by: printempw * @Last Modified by: printempw
* @Last Modified time: 2016-02-28 14:09:48 * @Last Modified time: 2016-03-12 18:28:07
*/ */
require "../includes/session.inc.php"; require "../includes/session.inc.php";
?> ?>