| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace App\Exceptions;
- use App\Http\Controllers\Controller;
- use App\Services\LogService;
- use Exception;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Http\Exceptions\HttpResponseException;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- 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 Exception
- */
- public function report(Throwable $exception)
- {
- parent::report($exception);
- }
- /**
- * Render an exception into an HTTP response.
- *
- * @param Request $request
- * @param Throwable $exception
- * @return \Symfony\Component\HttpFoundation\Response
- * @throws Throwable
- */
- public function render($request, Throwable $exception)
- {
- $errMsg='';
- try{
- $errMsg=(function()use($exception){
- return $errMsg=
- '异常: '
- .' code:'.(
- method_exists($exception,'getStatusCode')
- ?($exception->getStatusCode()??'')
- :'')
- .' message:'.(
- method_exists($exception,'getMessage')
- ?($exception->getMessage()??'')
- :'')
- .' trace:'.substr(
- method_exists($exception,'getTraceAsString')
- ?($exception->getTraceAsString()??'')
- :'',0,180);
- })();
- if(strpos($exception->getMessage(),'This action is unauthorized')!==false){
- return response()->view('exception.unauthorized');
- }
- if (method_exists($exception,'getStatusCode')) {
- $code = $exception->getStatusCode();
- switch ($code){
- case 419: $view='exception.login';break;
- case 404:$view='exception.404';break;
- default: $view='exception.default';break;
- }
- return response()->view($view,[
- 'code'=> $code,
- 'message'=>$exception->getMessage(),
- ]);
- }
- }catch (\Exception $e){}finally{
- $traces = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 4);
- LogService::log(
- trim(json_encode($traces[1]['class']??[]),"\""),
- trim(json_encode($traces[1]['function']??[]),"\""),
- ($errMsg??'')
- .'请求:'.json_encode($request->all())
- .'调用堆栈:'.json_encode($traces)
- );
- }
- return parent::render($request, $exception);
- }
- }
|