mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-14 11:20:53 +08:00
refactor: 规范Expection处理
This commit is contained in:
23
app/Exceptions/ApiException.php
Normal file
23
app/Exceptions/ApiException.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ApiException extends Exception
|
||||
{
|
||||
protected $code; // 错误码
|
||||
protected $message; // 错误消息
|
||||
protected $errors; // 全部错误信息
|
||||
|
||||
public function __construct($code = 400, $message = null, $errors = null)
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->code = $code;
|
||||
$this->errors = $errors;
|
||||
}
|
||||
public function errors(){
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
}
|
||||
19
app/Exceptions/BusinessException.php
Normal file
19
app/Exceptions/BusinessException.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class BusinessException extends Exception
|
||||
{
|
||||
/**
|
||||
* 业务异常构造函数
|
||||
* @param array $codeResponse 状态码
|
||||
* @param string $info 自定义返回信息,不为空时会替换掉codeResponse 里面的message文字信息
|
||||
*/
|
||||
public function __construct(array $codeResponse, $info = '')
|
||||
{
|
||||
[$code, $message] = $codeResponse;
|
||||
parent::__construct($info ?: $message, $code);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Helpers\ApiResponse;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Support\Arr;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -11,13 +12,15 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* A list of the exception types that are not reported.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dontReport = [
|
||||
//
|
||||
ApiException::class
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -55,7 +58,14 @@ class Handler extends ExceptionHandler
|
||||
public function render($request, Throwable $exception)
|
||||
{
|
||||
if ($exception instanceof ViewException) {
|
||||
abort(500, "主题渲染失败。如更新主题,参数可能发生变化请重新配置主题后再试。");
|
||||
$this->fail([500, '主题渲染失败。如更新主题,参数可能发生变化请重新配置主题后再试。']);
|
||||
}
|
||||
// ApiException主动抛出错误
|
||||
if ($exception instanceof ApiException) {
|
||||
$code = $exception->getCode();
|
||||
$message = $exception->getMessage();
|
||||
$errors = $exception->errors();
|
||||
return $this->fail([$code, $message],null,$errors);
|
||||
}
|
||||
return parent::render($request, $exception);
|
||||
}
|
||||
@@ -63,7 +73,6 @@ class Handler extends ExceptionHandler
|
||||
|
||||
protected function convertExceptionToArray(Throwable $e)
|
||||
{
|
||||
Log::channel("daily")->info($e);
|
||||
return config('app.debug') ? [
|
||||
'message' => $e->getMessage(),
|
||||
'exception' => get_class($e),
|
||||
|
||||
Reference in New Issue
Block a user