当前位置: 首页 > 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.xdnf.cn/news/1079443.html

相关文章:

  • 【第三章:神经网络原理详解与Pytorch入门】01.神经网络算法理论详解与实践-(2)神经网络整体结构
  • STM32-第二节-GPIO输入(按键,传感器)
  • C盘爆满元凶!WinSxS组件解密
  • JsonCpp的核心类及核心函数使用汇总
  • Web 服务器架构选择深度解析
  • Linux常见指令以及权限理解
  • Flowable12基础流程实战资金申请------------持续更新中
  • 埃及黑白沙漠:2亿年风蚀岩的“外星登陆现场“
  • 未来之窗冥界调试工具—东方仙盟
  • LTspice仿真10——电容
  • A模块 系统与网络安全 第四门课 弹性交换网络-1
  • 在小程序中实现实时聊天:WebSocket最佳实践
  • Verilog 语法介绍 1-1结构
  • Spring Boot + 本地部署大模型实现:基于 Ollama 的集成实践
  • KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
  • PHP语法基础篇(八):超全局变量
  • 转录组分析流程(零):流程介绍
  • 【二分】-----【Music Notes S】
  • 【Git】同时在本地使用多个github账号进行github仓库管理
  • 通过Curtain 解决方案保障BIM模型安全共享—建筑业的防泄密实战
  • react-打包和本地预览 ——打包优化
  • 【数据结构】C++的unordered_map/set模拟实现(开散列(哈希桶)作底层)
  • npm 命令入门指南(前端小白版)
  • contenteditable网页富文本编辑无法选中图片
  • 从0到1实战!用Docker部署Qwerty Learner输入法的完整实践过程
  • curl for android
  • Linux多线程(十三)之【POSIX信号量基于环形队列的生产消费模型】
  • OpenCV CUDA模块设备层-----在 GPU上高效地执行两个uint类型值的最小值比较函数vmin2()
  • LeetCode 317 最短距离选址问题详解(Swift 实现 + BFS 多源遍历)
  • 高边驱动 低边驱动