ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1.更改`app\lib\exception`下新建ExceptionHandler.php文件 ~~~ <?php namespace app\lib\exception; use Exception; use think\exception\Handle; use think\Request; class ExceptionHandler extends Handle { private $code; private $msg; private $errorCode; public function render(Exception $e) { if ($e instanceof BaseException) { //如果是自定义异常,则控制http状态码,不需要记录日志 //因为这些通常是因为客户端传递参数错误或者是用户请求造成的异常 //不应当记录日志 $this->code = $e->code; $this->msg = $e->msg; $this->errorCode = $e->errorCode; } else{ // 如果是服务器未处理的异常,将http状态码设置为500,并记录日志 $this->code = 500; //$this->msg = 'sorry,we make a mistake. (^o^)Y'; $this->msg = 'sorry,服务器内部错误, (^o^)Y'; $this->errorCode = 999; } $request = Request::instance(); $result = [ 'msg' => $this->msg, 'error_code' => $this->errorCode, 'request_url' => $request = $request->url() ]; return json($result, $this->code); } } ~~~ 2.更改model中的Banner.php ~~~ <?php namespace app\api\model; use think\Model; class Banner { public static function getBannerById($id) { return null; } } ~~~ 3.更改控制器中的Banner.php ~~~ <?php namespace app\api\controller\v1; use app\api\validate\IDMustBePostiveInt; use app\api\model\Banner as BannerModel; use app\lib\exception\BannerMissException; class Banner{ //获取指定id的banner信息 public function getBanner($id) { (new IDMustBePostiveInt())->goCheck(); $banner=BannerModel::getBannerByID($id); if(!$banner){ throw new BannerMissException(); } return $banner; } } ~~~ 结果 ![](https://box.kancloud.cn/1d59495162a294b530560568a1532e85_1096x512.png)