You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.0 KiB
PHTML
75 lines
2.0 KiB
PHTML
3 years ago
|
<?php
|
||
|
namespace app\common\exception;
|
||
|
|
||
|
use think\console\Output;
|
||
|
use think\exception\Handle;
|
||
|
use think\exception\HttpException;
|
||
|
use think\exception\ValidateException;
|
||
|
|
||
|
class ServiceHandle extends Handle
|
||
|
{
|
||
|
public $httpCode = 500;
|
||
|
public $code = 500;
|
||
|
public $message;
|
||
|
|
||
|
public function render(\Exception $e){
|
||
|
// 业务逻辑错误
|
||
|
if ($e instanceof ServiceException) {
|
||
|
$this->httpCode = $e->httpCode;
|
||
|
$this->code = $e->code;
|
||
|
$this->message = $e->message;
|
||
|
|
||
|
$data = [
|
||
|
'status' => $this->code,
|
||
|
'message' => $this->message,
|
||
|
'data' => [],
|
||
|
];
|
||
|
|
||
|
return json($data, $this->httpCode);
|
||
|
}
|
||
|
|
||
|
// 参数验证错误
|
||
|
if ($e instanceof ValidateException) {
|
||
|
return json($e->getError(), 422);
|
||
|
}
|
||
|
|
||
|
// 请求异常
|
||
|
if ($e instanceof HttpException && request()->isAjax()) {
|
||
|
return response($e->getMessage(), $e->getStatusCode());
|
||
|
}
|
||
|
|
||
|
// 其他错误交给系统处理
|
||
|
return parent::render($e);
|
||
|
}
|
||
|
|
||
|
public function renderForConsole(Output $output, \Exception $e)
|
||
|
{
|
||
|
// 业务逻辑错误
|
||
|
if ($e instanceof ServiceException) {
|
||
|
$this->httpCode = $e->httpCode;
|
||
|
$this->code = $e->code;
|
||
|
$this->message = $e->message;
|
||
|
|
||
|
$data = [
|
||
|
'status' => $this->code,
|
||
|
'message' => $this->message,
|
||
|
'data' => [],
|
||
|
];
|
||
|
|
||
|
return json($data, $this->httpCode);
|
||
|
}
|
||
|
|
||
|
// 参数验证错误
|
||
|
if ($e instanceof ValidateException) {
|
||
|
return json($e->getError(), 422);
|
||
|
}
|
||
|
|
||
|
// 请求异常
|
||
|
if ($e instanceof HttpException && request()->isAjax()) {
|
||
|
return response($e->getMessage(), $e->getStatusCode());
|
||
|
}
|
||
|
|
||
|
// 其他错误交给系统处理
|
||
|
return parent::render($e);
|
||
|
}
|
||
|
}
|