自定义后台master应用的异常是可以生效的,但是自定义以后原先的
'' => support\exception\Handler::class,失效了
我需要在访问后台的时候 master应用的异常是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
* 将相应的异常渲染到 HTTP 响应中
* @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://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
'' => support\exception\Handler::class,
'master' => app\exception\MasterHandler::class,
];
目录结构
我现在master应用里面的方法异常是ok的,但是/app/controller/indexController.php这个目录的异常处理却无效了,有没有什么办法,让默认的目录/app/controller/ 里面的控制器还是使用以前的异常处理或者我单独给他定义一个是如何定义呢?
感谢各位大佬!
问题已解决直接把全局的自定义一份即可,之所以自定义是想接口返回异常的时候格式化成json数据以方便前端处理
异常配置文件
新的自定义全局异常处理