php中的全局异常,ThinkPHP 5.1 全局异常处理接管

系统设置

config/app.php中修改全局异常处理类// 异常处理handle类 留空使用 \think\exception\Handle

'exception_handle' => '\\app\\common\\exception\\Http',

自定义类需要继承think\exception\Handle 并且实现render方法<?php

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');

自定义异常<?php

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'];

}

}

}<?php

namespace app\lib\exception;

class LoginInvalidException extends BaseException{

public $code= 403;

public $msg = '用户名或者密码错误';

public $errorCode = 403003;

}

控制器中使用<?php

/*

* @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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值