当前位置: 首页 > news >正文

thinkphp8接管异常处理类

创建文件app\Http.php

<?phpdeclare (strict_types = 1);namespace app;use ParseError;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ClassNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\InvalidArgumentException;
use think\exception\RouteNotFoundException;
use think\exception\ValidateException;
use think\facade\Log;
use think\Response;
use Throwable;class Http extends Handle
{public function render($request, Throwable $e): Response{// 404 - 访问资源不存在if (($e instanceof ClassNotFoundException || $e instanceof RouteNotFoundException) || ($e instanceof HttpException && 404 === $e->getStatusCode())) {Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getMessage()));}// 413 - 错误的数据类型或缺失参数if ($e instanceof InvalidArgumentException) {Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getFile() . '|第' . $e->getLine() . '行|' . $e->getMessage()));}// 422 - 参数验证错误if ($e instanceof ValidateException) {Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getError()));}// 500 - 语法错误if ($e instanceof ParseError) {Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getFile() . '|第' . $e->getLine() . '行|' . $e->getMessage()));}// 500 - 数据库错误if ($e instanceof PDOException || $e instanceof DbException) {Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getFile() . '|第' . $e->getLine() . '行|' . $e->getMessage()));}// 请求异常if ($e instanceof HttpException && $request->isAjax()) {Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getStatusCode() . ':' . $e->getMessage()));}// 其他错误Log::error(sprintf('[%s][%s] %s', __CLASS__, __FUNCTION__, $e->getFile() . '|第' . $e->getLine() . '行|' . $e->getMessage()));}
}

修改文件app\provider.php

<?php
use app\Http;
use app\Request;// 容器Provider定义文件
return ['think\Request' => Request::class,'think\exception\Handle' => Http::class,
];

http://www.dtcms.com/a/265728.html

相关文章:

  • linux系统 weblogic10.3.6(jar) 下载及安装
  • 后端 Maven打包 JAR 文件、前端打包dist文件、通过后端服务访问前端页面、Nginx安装与部署
  • Josn模块的使用
  • MVC 架构设计模式
  • Docker 安装 Redis 哨兵模式
  • 【数据结构】C++的unordered_map/set模拟实现(开散列(哈希桶)作底层)
  • 机器人“触摸”水果成熟度突破:SwishFormer模型与DIGIT视触觉传感器在HelloRobot上的水果检测应用
  • TDSQL如何查出某一列中的逗号数量
  • 从 TCP/IP 协议栈角度深入分析网络文件系统 (NFS)
  • (1)手摸手-学习 Vue3 之 Vite 创建项目
  • grpc 和限流Sentinel
  • STC8G 8051内核单片机开发(GPIO)
  • 2025年6月微短剧备案分析:都市题材占四成,20-29集成主流体量
  • OS15.【Linux】gdb调试器的简单使用
  • 修改文件属主
  • 活体检测api集成方案-炫彩活体检测助力身份核验
  • 马斯克脑机接口(Neuralink)技术进展,已经实现瘫痪患者通过BCI控制电脑、玩视频游戏、学习编程,未来盲人也能恢复视力了
  • [极客时间]LangChain 实战课 -----|(10) 链(下):想学“育花”还是“插花”?用RouterChain确定客户意图
  • 预警:病毒 “黑吃黑”,GitHub 开源远控项目暗藏后门
  • 2024年INS SCI2区,强化搜索自适应大邻域搜索算法RSALNS+无人机扩展型协作多任务分配,深度解析+性能实测
  • 实现如何利用 Kafka 延时删除 用户邮箱的验证码(如何发送邮箱+源码) - 第一期
  • 前缀和算法详解
  • FASTAPI+VUE3平价商贸管理系统
  • React自学 基础一
  • 基于大语言模型进行Prompt优化
  • 深入解析 AAC AudioSpecificConfig 在 RTSP/RTMP 播放器中的核心作用
  • PDF的图片文字识别工具
  • Spring AI ETL Pipeline使用指南
  • Java中的volatile到底是什么来路
  • OpenCV CUDA模块设备层-----在 GPU上高效地执行两个uint类型值的最小值比较函数vmin2()