国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

webman異常處理

sunwenzheng

webman異常處理

全局處理一般是用這個函數(shù) set_exception_handler
下面寫個例子

<?php 
namespace Demo;

// 自定義異常
class CustomException extends \Exception {
    public function errorMessage() {
        // 錯誤信息
        return "\nError on line " . $this->getLine() . " in " . $this->getFile()
             . ": " . $this->getMessage();
    }
}

// 自定義異常處理
class CustomExceptionHandler {
    public function render() {
        echo "\n";
        echo "CustomExceptionHandler";
        echo "\n";
    }
}

function handleException($exception) {
    echo "\n";
    echo "Caught exception: " . $exception->getMessage() . "\n";
    echo "\n".get_class($exception);
    // 這里可以做成一個k-v映射,或者按照一定規(guī)則對應(yīng)
    $handler = new (get_class($exception).'Handler')();
    $handler->render();

    echo $exception->errorMessage();
    echo "\n";
    echo "\n";
}

set_exception_handler('\Demo\handleException');

throw new \Demo\CustomException("Invalid data!");

輸出


Caught exception: Invalid data!

Demo\CustomException
CustomExceptionHandler

Error on line 34 in /project/webman/catch_error.php: Invalid data!

我一開始以為webman也是類似這樣的形式,

其實(shí)不是,webman是自己做了處理

webman的流程如下

主要是在 vendor/workerman/webman-framework/src/App.php 文件里

https://github.com/walkor/webman-framework/blob/97d8f4cb20e6a862ed1e0d4a236e5234f7aed0c0/src/App.php#L168

onMessage 方法 執(zhí)行 static::send($connection, $callback($request), $request);

一層層執(zhí)行完中間件后,執(zhí)行路由對應(yīng)的Controller,如果Controller這里拋出了異常

那么 回到那個 array_reduce 地方,捕捉到異常后,傳遞給異常處理器
截圖

https://github.com/walkor/webman-framework/blob/97d8f4cb20e6a862ed1e0d4a236e5234f7aed0c0/src/App.php#L354

異常處理器這里將參數(shù)給render方法
截圖
https://github.com/walkor/webman-framework/blob/97d8f4cb20e6a862ed1e0d4a236e5234f7aed0c0/src/App.php#L262

這個render 方法其實(shí) 是 ExceptionHandlerInterface 的,
ExceptionHandler 實(shí)現(xiàn)了 ExceptionHandlerInterface,
Handler 繼承 ExceptionHandler 又重寫了。

https://github.com/walkor/webman-framework/blob/97d8f4cb20e6a862ed1e0d4a236e5234f7aed0c0/src/support/exception/Handler.php#L39

到這里就很清楚了,
這個 Handler 就是 config/exception.php 里面的 support\exception\Handler::class 。
截圖

2652 0 4
0個評論

sunwenzheng

960
積分
0
獲贊數(shù)
0
粉絲數(shù)
2022-08-02 加入
??