ThinkPHP5異常處理類解析

在base.php文件中,用一句代碼\think\Error::register();實(shí)現(xiàn)錯(cuò)誤和異常處理機(jī)制的注冊(cè)健提。
// 注冊(cè)錯(cuò)誤和異常處理機(jī)制

\think\Error::register();

thinkphp\library\think\Error.php

namespace think;

use think\console\Output as ConsoleOutput;
use think\exception\ErrorException;
use think\exception\Handle;
use think\exception\ThrowableError;

class Error
{
    /**
     * 注冊(cè)異常處理
     * @access public
     * @return void
     */
    public static function register()
    {
        error_reporting(E_ALL);
        /*
           指定appError來(lái)處理系統(tǒng)錯(cuò)誤扎狱,捕獲錯(cuò)誤后把錯(cuò)誤以異常的形式拋出侧到。
           當(dāng)程序出現(xiàn)錯(cuò)誤的時(shí)候自動(dòng)調(diào)用appError函數(shù),
           該函數(shù)實(shí)例化了一個(gè)PHP自帶的錯(cuò)誤異常類ErrorException淤击,
           如果符合異常處理的匠抗,就將錯(cuò)誤信息以異常的形式拋出來(lái),否則將錯(cuò)誤信息寫入日志中
         */
        set_error_handler([__CLASS__, 'appError']);
        /*
            指定appException來(lái)處理用戶拋出的異常污抬,
            如果不是異常汞贸,就實(shí)例化ThrowableError類,將異常包裝起來(lái)并拋出
         */
        set_exception_handler([__CLASS__, 'appException']);
        /*
            指定appShutdown處理超時(shí)異常。
            注冊(cè)一個(gè)會(huì)在php中止時(shí)執(zhí)行的函數(shù) 注冊(cè)一個(gè)callback矢腻,它會(huì)在腳本執(zhí)行完成或者exit()后被調(diào)用门驾。
         */
        register_shutdown_function([__CLASS__, 'appShutdown']);
    }

    /**
     * 異常處理
     * @access public
     * @param  \Exception|\Throwable $e 異常
     * @return void
     */
    public static function appException($e)
    {
        if (!$e instanceof \Exception) {
            $e = new ThrowableError($e); 
        }

        //獲取handle類默認(rèn)handle類,你也可以在配置件文件的exception_handle項(xiàng)設(shè)置自己的handle類
        $handler = self::getExceptionHandler();
        $handler->report($e);
//將錯(cuò)誤記錄到日志

        if (IS_CLI) {
            $handler->renderForConsole(new ConsoleOutput, $e);//輸出錯(cuò)誤日志到cli窗口
        } else {
            $handler->render($e)->send();//輸出錯(cuò)誤日志到頁(yè)面
        }
    }

    /**
     * 錯(cuò)誤處理
     * @access public
     * @param  integer $errno      錯(cuò)誤編號(hào)
     * @param  integer $errstr     詳細(xì)錯(cuò)誤信息
     * @param  string  $errfile    出錯(cuò)的文件
     * @param  integer $errline    出錯(cuò)行號(hào)
     * @return void
     * @throws ErrorException
     */
    public static function appError($errno, $errstr, $errfile = '', $errline = 0)
    {
        $exception = new ErrorException($errno, $errstr, $errfile, $errline);

        // 符合異常處理的則將錯(cuò)誤信息托管至 think\exception\ErrorException
        if (error_reporting() & $errno) {
            throw $exception;
        }

        self::getExceptionHandler()->report($exception);
    }

    /**
     * 異常中止處理
     * @access public
     * @return void
     */
    public static function appShutdown()
    {
        // 將錯(cuò)誤信息托管至 think\ErrorException
        if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
            self::appException(new ErrorException(
                $error['type'], $error['message'], $error['file'], $error['line']
            ));
        }

        // 寫入日志
        Log::save();
    }

    /**
     * 確定錯(cuò)誤類型是否致命
     * @access protected
     * @param  int $type 錯(cuò)誤類型
     * @return bool
     */
    protected static function isFatal($type)
    {
        return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
    }

    /**
     * 獲取異常處理的實(shí)例
     * @access public
     * @return Handle
     */
    public static function getExceptionHandler()
    {
        static $handle;

        if (!$handle) {
            // 異常處理 handle
            $class = Config::get('exception_handle');

            if ($class && is_string($class) && class_exists($class) &&
                is_subclass_of($class, "\\think\\exception\\Handle")
            ) {
                $handle = new $class;
            } else {
                $handle = new Handle;

                if ($class instanceof \Closure) {
                    $handle->setRender($class);
                }

            }
        }

        return $handle;
    }
}

thinkphp\library\think\exception\Handle.php

namespace think\exception;

use Exception;
use think\App;
use think\Config;
use think\console\Output;
use think\Lang;
use think\Log;
use think\Response;

class Handle
{
    protected $render;
    protected $ignoreReport = [
        '\\think\\exception\\HttpException',
    ];

    public function setRender($render)
    {
        $this->render = $render;
    }

    /**
     * Report or log an exception.
     *
     * @param  \Exception $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        if (!$this->isIgnoreReport($exception)) {
            // 收集異常數(shù)據(jù)
            if (App::$debug) {
                $data = [
                    'file'    => $exception->getFile(),
                    'line'    => $exception->getLine(),
                    'message' => $this->getMessage($exception),
                    'code'    => $this->getCode($exception),
                ];
                $log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
            } else {
                $data = [
                    'code'    => $this->getCode($exception),
                    'message' => $this->getMessage($exception),
                ];
                $log = "[{$data['code']}]{$data['message']}";
            }

            if (Config::get('record_trace')) {
                $log .= "\r\n" . $exception->getTraceAsString();
            }

            Log::record($log, 'error');
        }
    }

    protected function isIgnoreReport(Exception $exception)
    {
        foreach ($this->ignoreReport as $class) {
            if ($exception instanceof $class) {
                return true;
            }
        }
        return false;
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Exception $e
     * @return Response
     */
    public function render(Exception $e)
    {
        if ($this->render && $this->render instanceof \Closure) {
            $result = call_user_func_array($this->render, [$e]);
            if ($result) {
                return $result;
            }
        }

        if ($e instanceof HttpException) {
            return $this->renderHttpException($e);
        } else {
            return $this->convertExceptionToResponse($e);
        }
    }

    /**
     * @param Output    $output
     * @param Exception $e
     */
    public function renderForConsole(Output $output, Exception $e)
    {
        if (App::$debug) {
            $output->setVerbosity(Output::VERBOSITY_DEBUG);
        }
        $output->renderException($e);
    }

    /**
     * @param HttpException $e
     * @return Response
     */
    protected function renderHttpException(HttpException $e)
    {
        $status   = $e->getStatusCode();
        $template = Config::get('http_exception_template');
        if (!App::$debug && !empty($template[$status])) {
            return Response::create($template[$status], 'view', $status)->assign(['e' => $e]);
        } else {
            return $this->convertExceptionToResponse($e);
        }
    }

    /**
     * @param Exception $exception
     * @return Response
     */
    protected function convertExceptionToResponse(Exception $exception)
    {
        // 收集異常數(shù)據(jù)
        if (App::$debug) {
            // 調(diào)試模式踏堡,獲取詳細(xì)的錯(cuò)誤信息
            $data = [
                'name'    => get_class($exception),
                'file'    => $exception->getFile(),
                'line'    => $exception->getLine(),
                'message' => $this->getMessage($exception),
                'trace'   => $exception->getTrace(),
                'code'    => $this->getCode($exception),
                'source'  => $this->getSourceCode($exception),
                'datas'   => $this->getExtendData($exception),
                'tables'  => [
                    'GET Data'              => $_GET,
                    'POST Data'             => $_POST,
                    'Files'                 => $_FILES,
                    'Cookies'               => $_COOKIE,
                    'Session'               => isset($_SESSION) ? $_SESSION : [],
                    'Server/Request Data'   => $_SERVER,
                    'Environment Variables' => $_ENV,
                    'ThinkPHP Constants'    => $this->getConst(),
                ],
            ];
        } else {
            // 部署模式僅顯示 Code 和 Message
            $data = [
                'code'    => $this->getCode($exception),
                'message' => $this->getMessage($exception),
            ];

            if (!Config::get('show_error_msg')) {
                // 不顯示詳細(xì)錯(cuò)誤信息
                $data['message'] = Config::get('error_message');
            }
        }

        //保留一層
        while (ob_get_level() > 1) {
            ob_end_clean();
        }

        $data['echo'] = ob_get_clean();

        ob_start();
        extract($data);
        include Config::get('exception_tmpl');
        // 獲取并清空緩存
        $content  = ob_get_clean();
        $response = new Response($content, 'html');

        if ($exception instanceof HttpException) {
            $statusCode = $exception->getStatusCode();
            $response->header($exception->getHeaders());
        }

        if (!isset($statusCode)) {
            $statusCode = 500;
        }
        $response->code($statusCode);
        return $response;
    }

    /**
     * 獲取錯(cuò)誤編碼
     * ErrorException則使用錯(cuò)誤級(jí)別作為錯(cuò)誤編碼
     * @param  \Exception $exception
     * @return integer                錯(cuò)誤編碼
     */
    protected function getCode(Exception $exception)
    {
        $code = $exception->getCode();
        if (!$code && $exception instanceof ErrorException) {
            $code = $exception->getSeverity();
        }
        return $code;
    }

    /**
     * 獲取錯(cuò)誤信息
     * ErrorException則使用錯(cuò)誤級(jí)別作為錯(cuò)誤編碼
     * @param  \Exception $exception
     * @return string                錯(cuò)誤信息
     */
    protected function getMessage(Exception $exception)
    {
        $message = $exception->getMessage();
        if (IS_CLI) {
            return $message;
        }

        if (strpos($message, ':')) {
            $name    = strstr($message, ':', true);
            $message = Lang::has($name) ? Lang::get($name) . strstr($message, ':') : $message;
        } elseif (strpos($message, ',')) {
            $name    = strstr($message, ',', true);
            $message = Lang::has($name) ? Lang::get($name) . ':' . substr(strstr($message, ','), 1) : $message;
        } elseif (Lang::has($message)) {
            $message = Lang::get($message);
        }
        return $message;
    }

    /**
     * 獲取出錯(cuò)文件內(nèi)容
     * 獲取錯(cuò)誤的前9行和后9行
     * @param  \Exception $exception
     * @return array                 錯(cuò)誤文件內(nèi)容
     */
    protected function getSourceCode(Exception $exception)
    {
        // 讀取前9行和后9行
        $line  = $exception->getLine();
        $first = ($line - 9 > 0) ? $line - 9 : 1;

        try {
            $contents = file($exception->getFile());
            $source   = [
                'first'  => $first,
                'source' => array_slice($contents, $first - 1, 19),
            ];
        } catch (Exception $e) {
            $source = [];
        }
        return $source;
    }

    /**
     * 獲取異常擴(kuò)展信息
     * 用于非調(diào)試模式html返回類型顯示
     * @param  \Exception $exception
     * @return array                 異常類定義的擴(kuò)展數(shù)據(jù)
     */
    protected function getExtendData(Exception $exception)
    {
        $data = [];
        if ($exception instanceof \think\Exception) {
            $data = $exception->getData();
        }
        return $data;
    }

    /**
     * 獲取常量列表
     * @return array 常量列表
     */
    private static function getConst()
    {
        return get_defined_constants(true)['user'];
    }
}

重寫Handle類

由上可知錯(cuò)誤將會(huì)由appShutdown猎唁、appError與appException接管,
并由Handle或者自定義Handle類report選擇組裝并輸出錯(cuò)誤
name我們?cè)囍远x錯(cuò)誤處理類重寫render
首先修改exception_handle

'exception_handle'=>'app\lib\exception\ExceptionHandle'

然后新建application/lib/exception/ExceptionHandler.php
負(fù)責(zé)渲染錯(cuò)誤信息

namespace app\lib\exception;
use think\Exception;
use think\exception\Handle;
use think\Request;
/**
 * 注意:
 * tp默認(rèn)調(diào)用的異常處理類是think\exception\Handle;
 * 調(diào)用異常處理類可以在config.php配置默認(rèn)為空 'exception_handle'=>'',調(diào)用的是 think\exception\Handle
 * 默認(rèn)調(diào)用application/lib/exception/下的這個(gè)類則需要修改配置為:'exception_handle'=>'app\lib\exception\ExceptionHandle'
 * 
 */
class ExceptionHandler extends Handle{

    private $code;//Http Code
    private $msg;
    private $errorCode;

    public function render(Exception $e){
        if ($e instanceof BaseException) {//G牦 =胗纭!帐偎!如果BaseException 與這個(gè)Exceptionhandler不是同一個(gè)命名空間一定要引入空間啊
            $this->code=$e->code;
            $this->msg=$e->message;
            $this->errorCode=$e->errorCode;
        }else{
            $this->code=500;
            $this->msg="服務(wù)器內(nèi)部錯(cuò)誤";
            $this->errorCode=999;//自定義的哦
        //這里手動(dòng)記錄日志

        }

        $request=Request::instance();
        $result=array(
            'msg'=>$this->msg,
            'error_code'=>$this->errorCode,
            'request_url'=>$request->url()
        );
        return json($result);
    }
}

為應(yīng)用定義一個(gè)公共異常類
然后新建application/lib/exception/BaseException.php
baseException定義code msg errorCode這三個(gè)屬性的默認(rèn)值

namespace app\lib\exception;
use think\Exception;
class BaseException extends Exception{
    //將錯(cuò)誤代碼設(shè)為http狀態(tài)碼逐纬,業(yè)務(wù)錯(cuò)誤代碼設(shè)為errorCode
    public $code=400;
    //錯(cuò)誤的具體信息
    public $message="參數(shù)錯(cuò)誤";
    //自定義的業(yè)務(wù)錯(cuò)誤碼
    public $errorCode="10000";
} 
//基類屬性參考
protected string $message ;//異常消息內(nèi)容
protected int $code ;//異常代碼
protected string $file ;//拋出異常的文件名
protected int $line ;//拋出異常在該文件中的行號(hào)

每個(gè)模塊每個(gè)錯(cuò)誤類型定義一個(gè)異常類繼承BaseException
application/lib/exception/BannerMissException.php

namespace app\lib\exception;
//use app\lib\exception\Handle;
class BannerMissException extends BaseException{
    //http狀態(tài)碼
    public $code=404;
    //錯(cuò)誤的具體信息
    public $message="請(qǐng)求的Banner不存在";
    //自定義的錯(cuò)誤碼
    public $errorCode=40000;

}

用法:

在model中查詢數(shù)據(jù)并在banner控制器調(diào)用 當(dāng)model中沒有數(shù)據(jù)時(shí)調(diào)用BannermissException 這時(shí)exceptionHandler的render將捕獲這個(gè)錯(cuò)誤
模塊

namespace app\api\Model;
use think\Model;
// use think\Excption;
class Banner extends Model{ 

    public static function getBannerById($id){
        //TODO:根據(jù)bannerid號(hào)獲取banner信息
        return null;
        //return "this is banner info";

    } 
}

控制器

namespace app\api\controller\v1;
use think\Controller;
use app\api\Model\Banner as BannerModel;//這里的Banner和Model的Banner重名
use app\lib\exception\BannerMissException;

class Banner extends controller{
    public function index(){
        //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index
    }
    public function  getBanner($id){

        $banner=BannerModel::getBannerById($id);
        if (!$banner) {
            throw new BannerMissException();
        }

    } 

}

異常類關(guān)系.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市削樊,隨后出現(xiàn)的幾起案子豁生,更是在濱河造成了極大的恐慌,老刑警劉巖漫贞,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甸箱,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡迅脐,警方通過(guò)查閱死者的電腦和手機(jī)芍殖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)谴蔑,“玉大人豌骏,你說(shuō)我怎么就攤上這事∫В” “怎么了窃躲?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)钦睡。 經(jīng)常有香客問(wèn)我蒂窒,道長(zhǎng),這世上最難降的妖魔是什么荞怒? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任洒琢,我火速辦了婚禮,結(jié)果婚禮上挣输,老公的妹妹穿的比我還像新娘福贞。我一直安慰自己撩嚼,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布恋技。 她就那樣靜靜地躺著蜻底,像睡著了一般薄辅。 火紅的嫁衣襯著肌膚如雪站楚。 梳的紋絲不亂的頭發(fā)上搏嗡,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天窿春,我揣著相機(jī)與錄音,去河邊找鬼采盒。 笑死旧乞,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的磅氨。 我是一名探鬼主播尺栖,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼悍赢!你這毒婦竟也來(lái)了决瞳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤左权,失蹤者是張志新(化名)和其女友劉穎皮胡,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赏迟,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屡贺,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锌杀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片甩栈。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖糕再,靈堂內(nèi)的尸體忽然破棺而出量没,到底是詐尸還是另有隱情,我是刑警寧澤突想,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布殴蹄,位于F島的核電站究抓,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏袭灯。R本人自食惡果不足惜刺下,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望稽荧。 院中可真熱鬧橘茉,春花似錦、人聲如沸姨丈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蟋恬。三九已至髓介,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間筋现,已是汗流浹背唐础。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留矾飞,地道東北人一膨。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像洒沦,于是被迫代替她去往敵國(guó)和親豹绪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容