model
l
<php?
namespace app\api\model;
use think\Exception;
class Banner
{
public static function getBannerByID($id){
//TODO:根据banner ID号,获取Banner信息
try{
1 / 0;
}catch (Exception $e){
//TODO 记录日志
throw $e;
}
return 'this is banner';
}
}
控制器
<?php
namespace app\api\controller\v1;
use app\api\validate\IDMustBePositiveInt;
use think\Exception;
use app\api\model\Banner as BannerModel;
class Banner
{
public function getBanner($id){
(new IDMustBePositiveInt())->goCheck();
try{
$banner = BannerModel::getBannerByID($id);
}catch (Exception $e){
$error = [
'error_code'=>10001,
'msg'=>$e->getMessage(),
];
return json($error,400);
}
return $banner;
}
}
客户端返回结果{"error_code":10001,"msg":"Division by zero"}