<?php
|
|
namespace app\common\exception;
|
|
use think\Exception;
|
use Throwable;
|
|
class ApiException extends Exception
|
{
|
public function __construct($message = "", $code = 1, $data = [])
|
{
|
$this->message = $message;
|
$this->code = $code;
|
$this->data = $data;
|
}
|
|
public function render(Exception $e)
|
{
|
$msg = $e->getMessage();
|
$code = 1;
|
// 验证异常
|
if ($e instanceof \think\exception\ValidateException) {
|
$code = 1;
|
$statuscode = 200;
|
$msg = $e->getError();
|
}
|
// Http异常
|
if ($e instanceof \think\exception\HttpException) {
|
$statuscode = $code = $e->getStatusCode();
|
}
|
return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => $this->data], 200);
|
}
|
|
}
|