From b4f1e12c00d561f4ac5d986174ebb5b689754988 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 22 Jul 2016 13:48:33 +0800 Subject: [PATCH] add custom error handler --- app/Exceptions/E.php | 2 +- app/Exceptions/ExceptionHandler.php | 54 ++++++++++++++++++++++++++++ app/Services/Http.php | 14 +++++--- index.php | 8 +++++ resources/views/errors/e.tpl | 10 ++++++ resources/views/errors/exception.tpl | 6 ++-- resources/views/errors/general.tpl | 3 +- 7 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 app/Exceptions/ExceptionHandler.php create mode 100644 resources/views/errors/e.tpl diff --git a/app/Exceptions/E.php b/app/Exceptions/E.php index d18bbaa9..0c8dd8ad 100644 --- a/app/Exceptions/E.php +++ b/app/Exceptions/E.php @@ -31,7 +31,7 @@ class E extends \Exception private function showErrorPage() { - echo \View::make('errors.exception')->with('code', $this->code)->with('message', $this->message); + echo \View::make('errors.e')->with('code', $this->code)->with('message', $this->message); exit; } } diff --git a/app/Exceptions/ExceptionHandler.php b/app/Exceptions/ExceptionHandler.php new file mode 100644 index 00000000..060ad94c --- /dev/null +++ b/app/Exceptions/ExceptionHandler.php @@ -0,0 +1,54 @@ +getCode()) { + case E_PARSE: + case E_ERROR: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + $level = 'Fatal Error'; + break; + + case E_WARNING: + case E_USER_WARNING: + case E_COMPILE_WARNING: + case E_RECOVERABLE_ERROR: + $level = 'Warning'; + break; + + case E_NOTICE: + case E_USER_NOTICE: + $level = 'Notice'; + break; + + case E_STRICT: + $level = 'Strict'; + break; + + case E_DEPRECATED: + case E_USER_DEPRECATED: + $level = 'Deprecated'; + break; + + default: + $level = 'Type Unknown'; + break; + } + + echo \View::make('errors.exception')->with('level', $level) + ->with('message', $e->getMessage()) + ->with('file', $e->getFile()) + ->with('line', $e->getLine()); + + exit; + + } + +} diff --git a/app/Services/Http.php b/app/Services/Http.php index f37d3e75..33bccbd0 100644 --- a/app/Services/Http.php +++ b/app/Services/Http.php @@ -46,12 +46,16 @@ class Http public static function getCurrentUrl() { - $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https://" : "http://"; - $url .= $_SERVER["SERVER_NAME"]; - $url .= ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); - $url .= $_SERVER["REQUEST_URI"]; + return self::getBaseUrl().$_SERVER["REQUEST_URI"]; + } - return $url; + public static function getBaseUrl() + { + $base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https://" : "http://"; + $base_url .= $_SERVER["SERVER_NAME"]; + $base_url .= ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); + + return $base_url; } public static function abort($code, $msg = "Something happened.", $is_json = false) diff --git a/index.php b/index.php index 1ce2a111..d508996a 100755 --- a/index.php +++ b/index.php @@ -23,8 +23,15 @@ if ($_ENV['APP_DEBUG'] !== "false") { new \Whoops\Handler\PrettyPageHandler : new \Whoops\Handler\PlainTextHandler; $whoops->pushHandler($handler); $whoops->register(); +} else { + set_error_handler(function ($errno, $errstr, $errfile, $errline) { + Exceptions\ExceptionHandler::handler( + new \ErrorException($errstr, $errno, $errno, $errfile, $errline) + ); + }); } +// set aliases for App\Services $services = require BASE_DIR.'/config/services.php'; foreach ($services as $facade => $class) { class_alias($class, $facade); @@ -51,6 +58,7 @@ $capsule->bootEloquent(); session_start(); +// require route config \Pecee\SimpleRouter\SimpleRouter::group([ 'exceptionHandler' => 'App\Exceptions\RouterExceptionHandler' ], function() { diff --git a/resources/views/errors/e.tpl b/resources/views/errors/e.tpl new file mode 100644 index 00000000..7340efb1 --- /dev/null +++ b/resources/views/errors/e.tpl @@ -0,0 +1,10 @@ +@extends('errors.general') + +@section('title', '出现错误') + +@section('content') +

{{ 出现了一些错误:}}

+ +

错误码: {{ $code }}

+

详细信息:{{ $message }}

+@endsection diff --git a/resources/views/errors/exception.tpl b/resources/views/errors/exception.tpl index fbc08583..d86f27b3 100644 --- a/resources/views/errors/exception.tpl +++ b/resources/views/errors/exception.tpl @@ -3,8 +3,10 @@ @section('title', '出现错误') @section('content') -

出现了一些错误:

+

{{ $level }}: 出现了一些错误

-

错误码: {{ $code }}

详细信息:{{ $message }}

+ +

文件位置:{{ $file }}: {{ $line }}

+ @endsection diff --git a/resources/views/errors/general.tpl b/resources/views/errors/general.tpl index 65fd3bef..50900468 100644 --- a/resources/views/errors/general.tpl +++ b/resources/views/errors/general.tpl @@ -5,8 +5,7 @@ @yield('title') - Blessing Skin Server - - +