jingcai-php/app/Exceptions/Handler.php

83 lines
2.2 KiB
PHP
Executable File

<?php
namespace App\Exceptions;
use App\Utils\Helps;
use App\Utils\Result;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Throwable
*/
public function report(Throwable $exception)
{
if (!($exception instanceof JingCaiException)) {
$uri = request()->getRequestUri();
if (strpos($uri, '/api') !== false ||
strpos($uri, '/upload') !== false) {
Log::error('SystemERROR', [
'uri' => $uri,
'request_data' => request()->all(),
'message' => $exception,
'useragent' => request()->userAgent(),
]);
} else {
parent::report($exception);
}
} else {
Log::info('JingCaiThrow', [
'uri' => request()->getRequestUri(),
'request_data' => request()->all(),
'message' => $exception->getMessage(),
'useragent' => request()->userAgent(),
]);
}
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
// 捕获全局自定义异常
if ($exception instanceof JingCaiException) {
return response()->json(Result::failed($exception->getMessage(), $exception->getCode()));
}
return parent::render($request, $exception);
}
}