webman_duanju/support/MyExceptionHandle.php

60 lines
1.7 KiB
PHP

<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support;
use app\exception\MyBusinessException;
use app\exception\SysException;
use Throwable;
use Webman\Exception\ExceptionHandler;
use Webman\Exception\ExceptionHandlerInterface;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\Exception\BusinessException;
/**
* Class Handler
* @package support\exception
*/
class MyExceptionHandle extends ExceptionHandler implements ExceptionHandlerInterface
{
public $dontReport = [
BusinessException::class,
];
public function report(Throwable $exception)
{
parent::report($exception);
}
public function render(Request $request, Throwable $exception): Response
{
if (!($exception instanceof MyBusinessException)) {
Log::error("捕获到全局异常,异常类,".$exception::class." 异常信息: ".$exception->getMessage());
if (!($exception instanceof SysException)) {
Log::error($exception->getTraceAsString());
}
return json( [
'code' => -1,
'message' => $exception->getMessage(),
'time' => time(),
'data' => [],
]);
}
// 非json请求则返回一个页面
return new Response(200, ['Content-Type' => 'application/json'], $exception->getMessage());
}
}