123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace app\common;
- use think\exception\Handle;
- use think\exception\HttpException;
- use think\exception\ValidateException;
- use think\exception\ErrorException;
- use think\Response;
- use Throwable;
- class Error extends Handle {
- public function render($request, Throwable $e): Response
- {
- $message ="数据正常";
- $code="0000";
- // 接管验证错误
- if($e instanceof ValidateException){
- $message =$e->getMessage();
- $code =1006;
- }
- if($e instanceof HttpException ){
- $message =$e->getMessage();
- $code =1006;
- }
- if($e instanceof ErrorException)
- {
- $message =$e->getMessage();
- $code =1006;
- }
- if($e instanceof \Exception)
- {
- $message =$e->getMessage();
- $code =$e->getCode();
- }
- if($e instanceof \Error )
- {
- $message =$e->getMessage();
- $code =1006;
- }
- // 添加自定义异常处理机制
- if(!empty($e)){
- return json(["message"=>$message,"code"=>$code]);
- }
- return parent::render($request, $e);
- }
- }
|