自定義后臺master應(yīng)用的異常是可以生效的,但是自定義以后原先的
'' => support\exception\Handler::class,失效了
我需要在訪問后臺的時候 master應(yīng)用的異常是ok的
但是也需要訪問原先 app/controller里面的控制器異常也ok該如何處理呢?
<?php
namespace app\exception;
use Throwable;
use PDOException;
use Illuminate\Database\QueryException;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;
class MasterHandler extends ExceptionHandler
{
/**
* Jacob
* 2025-02-10 17:00:00
* 不需要被記錄的異常
* @var array
*/
public $dontReport = [
//QueryException::class,
// Add other exceptions as needed
];
/**
* Jacob
* 2025-02-10 17:00:00
* 寫入到日志文件中
* @param Throwable $exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Jacob
* 2025-02-10 17:00:00
* 將相應(yīng)的異常渲染到 HTTP 響應(yīng)中
* @param Request $request
* @param Throwable $exception
* @return Response
*/
public function render(Request $request, Throwable $exception): Response
{
if ($exception instanceof PDOException || $exception instanceof QueryException) {
return json([
'code' => $exception->getCode(),
'msg' => $exception->getMessage(),
//'trace' => $exception->getTraceAsString()
]);
}
// Handle other exceptions
return parent::render($request, $exception);
}
}
/config/exception.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://wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
'' => support\exception\Handler::class,
'master' => app\exception\MasterHandler::class,
];
目錄結(jié)構(gòu)
我現(xiàn)在master應(yīng)用里面的方法異常是ok的,但是/app/controller/indexController.php這個目錄的異常處理卻無效了,有沒有什么辦法,讓默認(rèn)的目錄/app/controller/ 里面的控制器還是使用以前的異常處理或者我單獨給他定義一個是如何定義呢?
感謝各位大佬!
問題已解決直接把全局的自定義一份即可,之所以自定義是想接口返回異常的時候格式化成json數(shù)據(jù)以方便前端處理
異常配置文件
<?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://wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
'' => app\exception\Handler::class,//默認(rèn)全局異常處理
'master' => app\exception\MasterHandler::class,//后臺異常處理
];
新的自定義全局異常處理
<?php
namespace app\exception;
use Throwable;
use PDOException;
use Illuminate\Database\QueryException;
use Webman\Exception\ExceptionHandler;
use support\exception\PageNotFoundException;
use support\exception\MethodNotAllowedException;
use support\exception\BusinessException;
use support\exception\UnauthorizedException;
use support\exception\InvalidRequestException;
use support\exception\InvalidResponseException;
use support\exception\InvalidParameterException;
use support\exception\InvalidModelException;
use support\exception\InvalidTokenException;
use support\exception\InvalidSignatureException;
use support\exception\InvalidCredentialException;
use support\exception\InvalidPermissionException;
use support\exception\InvalidRoleException;
use support\exception\InvalidResourceException;
use support\exception\InvalidStateException;
use Webman\Http\Request;
use Webman\Http\Response;
class Handler extends ExceptionHandler
{
/**
* Jacob
* 2025-02-10 17:00:00
* 不需要被記錄的異常
* @var array
*/
public $dontReport = [
//QueryException::class,
// Add other exceptions as needed
];
/**
* Jacob
* 2025-02-10 17:00:00
* 寫入到日志文件中
* @param Throwable $exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Jacob
* 2025-02-10 17:00:00
* 將相應(yīng)的異常渲染到 HTTP 響應(yīng)中
* @param Request $request
* @param Throwable $exception
* @return Response
*/
public function render(Request $request, Throwable $exception): Response
{
if ($exception instanceof PDOException || $exception instanceof QueryException || $exception instanceof PageNotFoundException) {
return json([
'code' => $exception->getCode(),
'msg' => $exception->getMessage(),
//'trace' => $exception->getTraceAsString()
]);
}
// Handle other exceptions
return parent::render($request, $exception);
}
}