exceptionHandler.php
use Exception;
use think\exception\Handle;
use think\Request;
class ExceptionHandler extends Handle
{
public $code;
public $errorCode;
public $msg;
public function render(Exception KaTeX parse error: Expected '}', got 'EOF' at end of input: … {
if (e instanceof BaseException) {
$this->code = $e->code;
$this->errorCode = $e->errorCode;
$this->msg = $e->msg;
} else {
switch=config(′appdebug′);if(switch = config('app_debug');
if (switch=config(′appdebug′);if(switch) {
parent::render($e);
}else{
$this->code = 500;
$this->errorCode = 999;
$this->masg = ‘服务器内部错误’;
}
}
$result = [‘errorCode’ => $this->errorCode, ‘msg’ => this−>msg,′requesturl′=>Request::instance()−>url(),];returnjson(this->msg, 'request_url' => Request::instance()->url(),];
return json(this−>msg,′requesturl′=>Request::instance()−>url(),];returnjson(result, $this->code);
}
}
BaseException.php
use think\Exception;
- Class BaseException
- @package app\lib\exception
- 重写 全局异常处理基类: 用于统一描述错误信息:错误码、错误信息、当前URL
- 主要用于向客户端返回的错误信息,用于由于客户端的操作造成的错误,要向用户显示具体的错误信息(属于第一种错误类型)
*/
class BaseException extends Exception
{
//HTTP状态码
public $code = 400;
//自定义状态码
public $errorCode = 10000;//通用错误
//自定义状态码信息
public $msg = ‘参数错误’;
}
BannerMissException.php
class BannerMissException extends BaseException
{
//HTTP状态码
public $code = 404;
//自定义状态码
public $errorCode = 40000;//通用错误
//自定义状态码信息
public $msg = '请求Banner不存在';
}
api/controller/v1/Order.php
use app\api\model\builder\Order as OrderBuilderModel;
use app\api\validate\IDMustBePostiveInt;
use app\lib\exception\BannerMissException;
use app\lib\exception\OrderMimssEaception;
class Order
{
public function getOrder($id)
{
//AOP 面向切面编程
(new IDMustBePostiveInt())->goCheck();
result=OrderBuilderModel::get(result = OrderBuilderModel::get(result=OrderBuilderModel::get(id);
result=null;if(!result = null;
if (!result=null;if(!result) {
throw new BannerMissException();
}
return json($result);
}
}