| 
 
 在项目开发中,我们时常需要去监听全局抛出的异常信息,并在后台根据抛出的异常信息对bug进行修复。那么在Yii2中我们如何去设置呢  
1、首先我们需要先创建一个自定义异常类,用于处理全局异常  
<?php
namespace app\controllers;
use yii\web\Controller;
use Yii;
use yii\web\NotFoundHttpException;
use yii\web\Response;
class ExceptionController extends Controller
{
    public function __construct($id, $module, $config = [])
    {
        parent::__construct($id, $module, $config);
    }
    public function actionErrorHandler()
    {
        $exception = Yii::$app->errorHandler->exception;
	    //在这里我们可以将异常保存至异常队列,然后根据反馈信息处理异bug
		
		//对不同的异常抛出有不同的响应处理
        switch(get_class($exception))
        {
            case 'app\exceptions\ApiException':
                //响应json
                Yii::$app->response->format = Response::FORMAT_JSON;
                $data = [
                    'code'  =>  $exception->getCode(),
                    'msg'   =>  $exception->getMessage()
                ];
                return $data;
            case 'app\exceptions\HttpException':
                //响应视图
                return $this->render('error',[
                    'code'  =>  $exception->getCode(),
                    'msg'   =>  $exception->getMessage()
                ]);
            default:
                echo 'not get exception';
        }
        exit;
    }
}
  
2、我们需要在全局配置文件(app/config/web.php)中声明异常处理路径  
'components'=>[
	//自定义异常处理路径
	'errorHandler'	=>	'exception/error-handler'
]
  
3、创建两个自定义异常类,用于响应不同类型时,对结果处理。  ApiException(响应JSON数据格式)、HttpException(响应视图数据格式)  ApiException  
<?php
namespace app\exceptions;
use Throwable;
use yii\base\Exception;
class ApiException extends Exception
{
    public function __construct($message = "", $code = 0, Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
}
  
HttpException  
<?php
namespace app\exceptions;
use Throwable;
use yii\base\Exception;
class HttpException extends Exception
{
    public function __construct($message = "", $code = 0, Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
}
  
4、在入口文件(app\web\index.php)中,将当前模式切换为发布模式(目的是模拟发布环境上,确保异常的正常使用)  
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/web.php';
(new yii\web\Application($config))->run();
  
5、测试  
 public function actionIndex()
    {
        
        throw new ApiException('123',123);
        //throw new app\exceptions\HttpException('123',123);
       
    }
  
需要注意的点  1、在抛出异常之前不能更改当前响应数据的格式。例如  
Yii::$app->response->format = Response::FORMAT_JSON;
  
如果这样设置了,那么会导致Yii2抓取不了异常信息 
                
                
                
        
        
    
  
 
 |