系统设置
config/app.php中修改全局异常处理类// 异常处理handle类 留空使用 \think\exception\Handle
'exception_handle' => '\\app\\common\\exception\\Http',
自定义类需要继承think\exception\Handle 并且实现render方法
namespace app\lib\exception;
use Exception;
use think\exception\Handle;
use think\facade\Request;
class ExceptionHandle extends Handle
{
private $code;
private $msg;
private $errorCode;
private $url;
public function render(Exception $e)
{
if($e instanceof BaseException){
$this->code = $e->code;
$this->msg = $e->msg;
$this->errorCode = $e->errorCode;
} else {
if(config('app.app_debug')){
//开启调试模式的时候返回系统自带的那个错误页面
//没开启调试模式的时候返回自定义错误
return parent::render($e);
}
$this->code = 500;
$this->msg = '系统故障,请联系管理员';
$this->errorCode=999;
}
$this->url=Request::url(true);
$result = [
'error_code'=>$this->errorCode,
'msg'=>$this->msg,
'url'=>$this->url
];
return json($result,$this->code);
}
}
用系统自带异常类抛出异常// 使用think自带异常类抛出异常
throw new \think\Exception('异常消息', 10006);
// 使用助手函数抛出异常
exception('异常消息', 10006);
// 抛出自定义异常
exception('异常消息', 10006,'\app\common\exception\NotFoundException');
自定义异常
namespace app\lib\exception;
use think\Exception;
class BaseException extends Exception {
public $code =400;
public $msg = '参数错误';
public $errorCode =999;
public function __construct($params=[]){
if(!is_array($params)){
return;
}
if(array_key_exists('code',$params)){
$this->code = $params['code'];
}
if(array_key_exists('msg',$params)){
$this->code = $params['msg'];
}
if(array_key_exists('errorCode',$params)){
$this->code = $params['errorCode'];
}
}
}
namespace app\lib\exception;
class LoginInvalidException extends BaseException{
public $code= 403;
public $msg = '用户名或者密码错误';
public $errorCode = 403003;
}
控制器中使用
/*
* @Description: 登录
* @Version: 1.0
* @Autor: Yinux
* @Date: 2020-04-10 19:49:31
* @LastEditors: Yinux
* @LastEditTime: 2020-04-23 15:55:44
*/
namespace app\api\controller;
use think\Controller;
use app\api\service\Login as LoginService;
use app\api\controller\Token;
use app\lib\exception\LoginInvalidException;
use app\api\model\Trader as TraderModel;
class Login extends Controller
{
public function wxLogin()
{
$mobile=$this->request->param('mobile');
$password=$this->request->param('password', '');
$client=LoginService::wxLogin($mobile, $password);
if ($client) {
$token=new Token();
$token=$token->mkToken(1, $client->traderid, $client->shopid);
$data=[
'token'=>$token,
'client'=>$client
];
return $data;
} else {
throw new LoginInvalidException();
}
}
}
php自带异常类Exception {
/* 属性 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 方法 */
public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public getMessage ( void ) : string
final public getPrevious ( void ) : Throwable
final public getCode ( void ) : int
final public getFile ( void ) : string
final public getLine ( void ) : int
final public getTrace ( void ) : array
final public getTraceAsString ( void ) : string
public __toString ( void ) : string
final private __clone ( void ) : void
}