|
...
|
...
|
@@ -2,10 +2,19 @@ |
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Enums\Common\Common;
|
|
|
|
use App\Services\DingService;
|
|
|
|
use App\Traits\RedisTrait;
|
|
|
|
use App\Utils\EncryptUtils;
|
|
|
|
use App\Utils\LogUtils;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
|
|
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
...
|
...
|
@@ -13,7 +22,7 @@ class Handler extends ExceptionHandler |
|
|
|
/**
|
|
|
|
* A list of the exception types that are not reported.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @var array<int, class-string<Throwable>>
|
|
|
|
*/
|
|
|
|
protected $dontReport = [
|
|
|
|
//
|
|
...
|
...
|
@@ -22,14 +31,26 @@ class Handler extends ExceptionHandler |
|
|
|
/**
|
|
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @var array<int, string>
|
|
|
|
*/
|
|
|
|
protected $dontFlash = [
|
|
|
|
'current_password',
|
|
|
|
'password',
|
|
|
|
'password_confirmation',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the exception handling callbacks for the application.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
$this->reportable(function (Throwable $e) {
|
|
|
|
//
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Report or log an exception.
|
|
|
|
*
|
|
|
|
* @param \Throwable $exception
|
|
...
|
...
|
@@ -39,9 +60,41 @@ class Handler extends ExceptionHandler |
|
|
|
*/
|
|
|
|
public function report(Throwable $exception)
|
|
|
|
{
|
|
|
|
parent::report($exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
//日志记录
|
|
|
|
$exceptionMessage = "错误CODE:" . $exception->getCode() .
|
|
|
|
"-----错误message:" . $exception->getMessage() .
|
|
|
|
'------错误文件:' . $exception->getFile() .
|
|
|
|
'-------错误行数:' . $exception->getLine();
|
|
|
|
//A端错误
|
|
|
|
if ($exception instanceof AsideGlobalException) {
|
|
|
|
LogUtils::error("AsideGlobalException", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
//B端错误
|
|
|
|
elseif($exception instanceof BsideGlobalException) {
|
|
|
|
LogUtils::error("BsideGlobalException", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
//验证错误(非手动抛出)
|
|
|
|
elseif ($exception instanceof ValidationException) {
|
|
|
|
LogUtils::error("参数验证失败", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
//Redis错误(非手动抛出)
|
|
|
|
elseif ($exception instanceof \RedisException) {
|
|
|
|
LogUtils::error("Redis服务异常", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
//Mysql错误(非手动抛出)
|
|
|
|
elseif ($exception instanceof \PDOException) {
|
|
|
|
LogUtils::error("数据库服务异常", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
//模型未找到错误(非手动抛出)
|
|
|
|
elseif ($exception instanceof ModelNotFoundException) {
|
|
|
|
LogUtils::error("模型资源未找到", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
//其他
|
|
|
|
else {
|
|
|
|
LogUtils::error("全局系统异常", [], $exceptionMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
|
...
|
...
|
@@ -53,6 +106,53 @@ class Handler extends ExceptionHandler |
|
|
|
*/
|
|
|
|
public function render($request, Throwable $exception)
|
|
|
|
{
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
$message = $exception->getMessage();
|
|
|
|
|
|
|
|
if ($exception instanceof AsideGlobalException) {
|
|
|
|
$code = $exception->getCode();
|
|
|
|
}elseif ($exception instanceof BsideGlobalException) {
|
|
|
|
$code = $exception->getCode();
|
|
|
|
} elseif ($exception instanceof ValidationException) {
|
|
|
|
$code = Code::USER_PARAMS_ERROE();
|
|
|
|
} elseif ($exception instanceof \RedisException) {
|
|
|
|
$code = Code::SERVER_REDIS_ERROR();
|
|
|
|
} elseif ($exception instanceof \PDOException) {
|
|
|
|
$code = Code::SERVER_MYSQL_ERROR();
|
|
|
|
} elseif ($exception instanceof ModelNotFoundException) {
|
|
|
|
$code = Code::USER_MODEL_NOTFOUND_ERROE();
|
|
|
|
} elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
|
|
|
|
return response('404 Not Found', 404);
|
|
|
|
} else {
|
|
|
|
$code = Code::SYSTEM_ERROR();
|
|
|
|
}
|
|
|
|
//钉钉通知错误信息
|
|
|
|
if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) {
|
|
|
|
$exceptionMessage = "路由:" . Route::current()->uri .
|
|
|
|
"-----错误CODE:" . $exception->getCode() .
|
|
|
|
"-----错误message:" . $exception->getMessage() .
|
|
|
|
'------错误文件:' . $exception->getFile() . '-------错误行数:' .
|
|
|
|
$exception->getLine();
|
|
|
|
(new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]);
|
|
|
|
}
|
|
|
|
//开启debug 错误原样输出
|
|
|
|
$debub = config('app.debug');
|
|
|
|
$message = $debub ? $message : ($code->description ?? $message);
|
|
|
|
|
|
|
|
//请求参数错误 输出具体错误信息
|
|
|
|
if($code == Code::USER_PARAMS_ERROE()){
|
|
|
|
$message = Arr::first(Arr::first($exception->errors()));
|
|
|
|
}
|
|
|
|
$response = [
|
|
|
|
'code' => $code,
|
|
|
|
'message' => $message
|
|
|
|
];
|
|
|
|
//加密返回
|
|
|
|
if (config('app.params_encrypt')) {
|
|
|
|
$k = config('app.params_encrypt_key');
|
|
|
|
$i = config('app.params_encrypt_iv');
|
|
|
|
$response = [
|
|
|
|
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
|
|
|
|
}
|
|
|
|
return response($response);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|