add PrettyPageException to handle exceptions which should be rendered

This commit is contained in:
printempw 2016-08-29 19:47:30 +08:00
parent 124b98f9d7
commit bde548de40
17 changed files with 77 additions and 53 deletions

View File

@ -46,6 +46,13 @@ class Handler extends ExceptionHandler
$e = new NotFoundHttpException($e->getMessage(), $e);
}
if ($e instanceof PrettyPageException && PHP_SAPI != "cli") {
echo \View::make('errors.e')->with('code', $e->getCode())
->with('message', $e->getMessage())
->render();
exit;
}
if (config('app.debug')) {
foreach ($this->dontReport as $type) {
if ($e instanceof $type) {

View File

@ -0,0 +1,8 @@
<?php
namespace App\Exceptions;
class PrettyPageException extends \Exception
{
}

View File

@ -8,7 +8,7 @@ use App\Models\UserModel;
use App\Models\Player;
use App\Models\PlayerModel;
use App\Models\Texture;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Validate;
use Utils;
use View;
@ -135,7 +135,7 @@ class AdminController extends BaseController
$cur_user = new User(session('uid'));
if (!$user->is_registered)
throw new E('用户不存在', 1);
View::json('用户不存在', 1);
if ($action == "email") {
Validate::checkPost(['email']);
@ -210,7 +210,7 @@ class AdminController extends BaseController
View::json('账号已被成功删除', 0);
} else {
throw new E('非法参数', 1);
View::json('非法参数', 1);
}
}
@ -263,7 +263,7 @@ class AdminController extends BaseController
if (PlayerModel::where('pid', $_POST['pid'])->delete())
View::json('角色已被成功删除', 0);
} else {
throw new E('非法参数', 1);
View::json('非法参数', 1);
}
}

View File

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Models\User;
use App\Models\UserModel;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Validate;
use Mail;
use View;
@ -76,7 +76,7 @@ class AuthController extends BaseController
View::json('登出成功~', 0);
} else {
throw new E('并没有有效的 session', 1);
View::json('并没有有效的 session', 1);
}
}
@ -85,7 +85,7 @@ class AuthController extends BaseController
if (Option::get('user_can_register') == 1) {
return view('auth.register');
} else {
throw new E('残念。。本皮肤站已经关闭注册咯 QAQ', 7, true);
throw new PrettyPageException('残念。。本皮肤站已经关闭注册咯 QAQ', 7);
}
}
@ -137,7 +137,7 @@ class AuthController extends BaseController
if ($_ENV['MAIL_HOST'] != "") {
return view('auth.forgot');
} else {
throw new E('本站已关闭重置密码功能', 8, true);
throw new PrettyPageException('本站已关闭重置密码功能', 8);
}
}

View File

@ -7,7 +7,7 @@ use App\Models\User;
use App\Models\Texture;
use App\Models\Closet;
use App\Models\ClosetModel;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use View;
use Option;
@ -59,7 +59,7 @@ class ClosetController extends BaseController
public function remove()
{
if (!is_numeric(\Utils::getValue('tid', $_POST)))
throw new E('非法参数', 1);
View::json('非法参数', 1);
if ($this->closet->remove($_POST['tid'])) {
$t = Texture::find($_POST['tid']);

View File

@ -7,7 +7,7 @@ use App\Models\User;
use App\Models\Player;
use App\Models\PlayerModel;
use App\Models\Texture;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Validate;
use Utils;
use Option;
@ -87,7 +87,7 @@ class PlayerController extends BaseController
$new_player_name = Utils::getValue('new_player_name', $_POST);
if (!$new_player_name)
throw new E('非法参数', 1);
View::json('非法参数', 1);
if (!Validate::playerName($new_player_name))
{
@ -114,7 +114,7 @@ class PlayerController extends BaseController
$tid = Utils::getValue('tid', $_POST);
if (!is_numeric($tid))
throw new E('非法参数', 1);
View::json('非法参数', 1);
if (!($texture = Texture::find($tid)))
View::json('Unexistent texture.', 6);
@ -140,7 +140,7 @@ class PlayerController extends BaseController
if (!isset($_POST['preference']) ||
($_POST['preference'] != "default" && $_POST['preference'] != "slim"))
{
throw new E('非法参数', 1);
View::json('非法参数', 1);
}
$this->player->setPreference($_POST['preference']);

View File

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Models\User;
use App\Models\Texture;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Validate;
use Option;
use Utils;

View File

@ -8,7 +8,7 @@ use App\Events\GetAvatarPreview;
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Storage;
use Minecraft;
use Option;

View File

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Models\User;
use App\Models\Texture;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Utils;
use View;
@ -48,7 +48,7 @@ class UserController extends BaseController
{
// handle changing nickname
if ($this->action == "nickname") {
if (!isset($_POST['new_nickname'])) throw new E('非法参数');
if (!isset($_POST['new_nickname'])) View::json('非法参数', 1);
if (Utils::convertString($_POST['new_nickname']) != $_POST['new_nickname'])
View::json('无效的昵称。昵称中包含了奇怪的字符。', 1);
@ -58,7 +58,7 @@ class UserController extends BaseController
// handle changing password
} elseif ($this->action == "password") {
if (!(isset($_POST['current_password']) && isset($_POST['new_password'])))
throw new E('非法参数');
View::json('非法参数', 1);
if (!$this->user->checkPasswd($_POST['current_password']))
View::json('原密码错误', 1);
@ -70,7 +70,7 @@ class UserController extends BaseController
// handle changing email
} elseif ($this->action == "email") {
if (!(isset($_POST['new_email']) && isset($_POST['password'])))
throw new E('非法参数');
View::json('非法参数', 1);
if (!filter_var($_POST['new_email'], FILTER_VALIDATE_EMAIL)) {
View::json('邮箱格式错误', 3);
@ -85,7 +85,7 @@ class UserController extends BaseController
// handle deleting account
} elseif ($this->action == "delete") {
if (!isset($_POST['password']))
throw new E('非法参数');
View::json('非法参数', 1);
if (!$this->user->checkPasswd($_POST['password']))
View::json('密码错误', 1);
@ -109,17 +109,18 @@ class UserController extends BaseController
public function setAvatar()
{
if (!isset($_POST['tid'])) throw new E('Empty tid.');
if (!isset($_POST['tid']))
View::json('Empty tid.', 1);
$result = Texture::find($_POST['tid']);
if ($result) {
if ($result->type == "cape") throw new E('披风可不能设置为头像哦~', 1);
if ($result->type == "cape") View::json('披风可不能设置为头像哦~', 1);
if ((new User(session('uid')))->setAvatar($_POST['tid'])) {
View::json('设置成功!', 0);
}
} else {
throw new E('材质不存在。', 1);
View::json('材质不存在。', 1);
}
}

View File

@ -4,7 +4,7 @@ namespace App\Http\Middleware;
use App\Models\User;
use App\Models\UserModel;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use View;
use Http;
use Session;
@ -31,7 +31,7 @@ class CheckAuthenticated
Session::flush();
Session::save();
throw new E('你已经被本站封禁啦,请联系管理员解决', 5, true);
throw new PrettyPageException('你已经被本站封禁啦,请联系管理员解决', 5);
}
// ask for filling email

View File

@ -2,7 +2,7 @@
namespace App\Http\Middleware;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Validate;
use Utils;
use View;

View File

@ -2,8 +2,9 @@
namespace App\Models;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Utils;
use View;
class Closet
{
@ -93,7 +94,7 @@ class Closet
{
foreach ($this->textures as $item) {
if ($item['tid'] == $tid)
throw new E('你已经收藏过这个材质啦', 1);
View::json('你已经收藏过这个材质啦', 1);
}
$this->textures[] = array(
@ -137,7 +138,7 @@ class Closet
$offset++;
}
throw new E('The texture is not in the closet.', 1);
View::json('The texture is not in the closet.', 1);
}
private function checkTextureExist($tid)

View File

@ -4,9 +4,10 @@ namespace App\Models;
use App\Events\PlayerProfileUpdated;
use App\Events\GetPlayerJson;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Event;
use Utils;
use View;
class Player
{
@ -68,7 +69,7 @@ class Player
{
if (!isset($tids['tid_steve']) && !isset($tids['tid_alex']) && !isset($tids['tid_cape']))
{
throw new E('非法参数', 1);
View::json('非法参数', 1);
}
$this->model->tid_steve = isset($tids['tid_steve']) ? $tids['tid_steve'] : $this->model['tid_steve'];
@ -144,7 +145,7 @@ class Player
if ($api_type == self::CSL_API || $api_type == self::USM_API) {
return \Storage::disk('cache')->get("json/{$this->pid}-{$api_type}");
} else {
throw new E('不支持的 API_TYPE。', -1, true);
throw new PrettyPageException('不支持的 API_TYPE。', -1);
}
}

View File

@ -3,7 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
class BootServiceProvider extends ServiceProvider
{
@ -15,10 +15,18 @@ class BootServiceProvider extends ServiceProvider
public function boot()
{
\View::addExtension('tpl', 'blade');
$this->checkFileExists();
$this->checkDbConfig();
$this->checkInstallation();
}
protected function checkFileExists()
{
if (!file_exists(BASE_DIR."/.env")) {
throw new PrettyPageException('.env 文件不存在', -1);
}
}
protected function checkDbConfig()
{
// use error control to hide shitty connect warnings
@ -31,7 +39,7 @@ class BootServiceProvider extends ServiceProvider
);
if ($conn->connect_error)
throw new E("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno, true);
throw new PrettyPageException("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno);
return true;
}
@ -44,7 +52,7 @@ class BootServiceProvider extends ServiceProvider
if (!is_dir(BASE_DIR.'/storage/textures/')) {
if (!mkdir(BASE_DIR.'/storage/textures/'))
throw new E('textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。', -1);
throw new PrettyPageException('textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。', -1);
}
if (config('app.version') != \Option::get('version', '')) {

View File

@ -2,13 +2,10 @@
namespace App\Services\Database;
use \App\Exceptions\E;
use \Blessing\Config;
/**
* Light-weight database helper
*
* @author <h@prinzeugen.net>
* @author <h@prinzeugen.net>
*/
class Database
{
@ -52,7 +49,7 @@ class Database
);
if ($this->connection->connect_error)
throw new E("Could not connect to MySQL database. Check your configuration:".
throw new \InvalidArgumentException("Could not connect to MySQL database. Check your configuration:".
$this->connection->connect_error, $this->connection->connect_errno, true);
$this->connection->query("SET names 'utf8'");
@ -75,7 +72,7 @@ class Database
$result = $this->connection->query($sql);
if ($this->connection->error)
throw new E("Database query error: ".$this->connection->error.", Statement: ".$sql, -1);
throw new \Exception("Database query error: ".$this->connection->error.", Statement: ".$sql, -1);
return $result;
}
@ -90,9 +87,9 @@ class Database
*
* @param string $key
* @param string $value
* @param array $condition, see function `where`
* @param string $table, which table to operate
* @param boolean $dont_fetch_array, return resources if true
* @param array $condition See function `where`
* @param string $table Which table to operate
* @param bool $dont_fetch_array Return resources if true
* @return array|resources
*/
public function select($key, $value, $condition = null, $table = null, $dont_fetch_array = false)
@ -173,7 +170,7 @@ class Database
/**
* Generate where statement
*
* @param array $condition, e.g. array('where'=>'username="shit"', 'limit'=>10, 'order'=>'uid')
* @param array $condition e.g. array('where'=>'username="shit"', 'limit'=>10, 'order'=>'uid')
* @return string
*/
private function where($condition)

View File

@ -2,7 +2,7 @@
namespace App\Services;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use Storage;
class Utils

View File

@ -2,7 +2,8 @@
namespace App\Services;
use App\Exceptions\E;
use App\Exceptions\PrettyPageException;
use View;
class Validate
{
@ -17,7 +18,7 @@ class Validate
foreach ($keys as $key) {
if (!isset($_POST[$key])) {
if ($silent) return false;
throw new E('非法参数', 1);
View::json('非法参数', 1);
}
}
return true;
@ -43,9 +44,9 @@ class Validate
public static function textureName($texture_name)
{
if (strlen($texture_name) > 32 || strlen($texture_name) < 1) {
throw new E('无效的材质名称。材质名长度应该小于 32。', 2);
View::json('无效的材质名称。材质名长度应该小于 32。', 2);
} else if (Utils::convertString($texture_name) != $texture_name) {
throw new E('无效的材质名称。材质名称中包含了奇怪的字符。', 2);
View::json('无效的材质名称。材质名称中包含了奇怪的字符。', 2);
}
return true;
}
@ -54,10 +55,10 @@ class Validate
{
if (strlen($password) > 16 || strlen($password) < 8) {
if ($silent) return false;
throw new E('无效的密码。密码长度应该大于 8 并小于 16。', 2);
View::json('无效的密码。密码长度应该大于 8 并小于 16。', 2);
} else if (Utils::convertString($password) != $password) {
if ($silent) return false;
throw new E('无效的密码。密码中包含了奇怪的字符。', 2);
View::json('无效的密码。密码中包含了奇怪的字符。', 2);
}
return true;
}