审查视图

application/api/library/ExceptionHandle.php 957 字节
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

namespace app\api\library;

use Exception;
use think\exception\Handle;

/**
 * 自定义API模块的错误显示
 */
class ExceptionHandle extends Handle
{

    public function render(Exception $e)
    {
        // 在生产环境下返回code信息
17
        if (!\think\Config::get('app_debug')) {
18 19 20
            $statuscode = $code = 500;
            $msg = 'An error occurred';
            // 验证异常
21
            if ($e instanceof \think\exception\ValidateException) {
22 23 24 25 26
                $code = 0;
                $statuscode = 200;
                $msg = $e->getError();
            }
            // Http异常
27
            if ($e instanceof \think\exception\HttpException) {
28 29 30 31 32 33 34 35 36 37
                $statuscode = $code = $e->getStatusCode();
            }
            return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
        }

        //其它此交由系统处理
        return parent::render($e);
    }

}