首页 > 其他分享 >webman:配置异常处理返回json格式(v1.5.7)

webman:配置异常处理返回json格式(v1.5.7)

时间:2023-08-23 09:46:21浏览次数:58  
标签:exception webman use ExceptionHandler json v1.5 Throwable com

一,添加一个除0错的异常代码:

页面显示效果如图:

二,配置:

php代码

1,config/

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?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<[email protected]>  * @copyright walkor<[email protected]>  * @link      http://www.workerman.net/  * @license   http://www.opensource.org/licenses/mit-license.php MIT License  */ //指定异常处理类的路径 return [     //'' => support\exception\Handler::class,     '' => app\exception\ExceptionHandler::class, ];

2,app/exception/ExceptionHandler.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <?php   namespace app\exception;   use Webman\Exception\ExceptionHandler as Handle; use Webman\Http\Request; use Webman\Http\Response;   use Throwable;   class ExceptionHandler extends Handle {     private $code;     private $status;     private $msg;       /**      * 记录异常信息(包括日志或者其它方式记录)      *      * @access public      * @param Throwable $exception      * @return void      */     public function report(Throwable $exception): void     {         // 使用内置的方式记录异常日志         parent::report($exception);     }       public function render(Request $request, Throwable $e): Response     {         $result = [             'code' => $e->getCode(),             'message'=>$e->getMessage(),             'file'=>$e->getFile(),             'uri'=>$request->uri(),             'url'=>$request->fullUrl(),         ];           return json($result);     } }

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/19/webman-pei-zhi-yi-chang-chu-li-fan-hui-json-ge-shi/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

三,测试效果:

四,查看webman版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name     : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...

标签:exception,webman,use,ExceptionHandler,json,v1.5,Throwable,com
From: https://www.cnblogs.com/architectforest/p/17650280.html

相关文章

  • JSON文件如何添加注释?
    JSON(JavaScriptObjectNotation)的设计是简洁和明确的,它不支持注释。这是JSON设计的决策之一,目的是使JSON保持尽可能简单和纯粹。但在实践中,很多时候,开发者确实会想在JSON文件中添加注释,以帮助解释或标记某些部分。虽然JSON标准不支持注释,但你仍然可以采取以下策略:使用替代文件:使用......
  • Json 格式处理
    Jackson1.maven依赖导入<!--https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId>......
  • flask接收json,开启服务demo
    demofromflaskimportFlask,render_template,request,jsonifyapp=Flask(__name__)@app.route('/',methods=['GET','POST'])defindex():processed_data=Noneifrequest.method=='POST':try......
  • 4.8 C++ Boost 应用JSON解析库
    property_tree是Boost库中的一个头文件库,用于处理和解析基于XML、Json或者INFO格式的数据。property_tree可以提供一个轻量级的、灵活的、基于二叉数的通用容器,可以处理包括简单值(如int、float)和复杂数据结构(如结构体和嵌套容器)在内的各种数据类型。它可以解析数据文件到......
  • c#json处理字符串
    usingNewtonsoft.Json;//定义一个JSON字符串stringjsonStr="{\"username\":\"test\",\"age\":18}";//将JSON字符串解析成对象dynamicobj=JsonConvert.DeserializeObject(jsonStr);//访问对象属性stringname=obj.username......
  • json-schema编辑器(json-schema-editor)
     最近在找一个 json-schema的编辑器,在网上找了找,以下两个项目用的比较多一、两款json-schema-editor1、vue-json-schema-editor-visual一个高效易用的基于Vue+ElementUI的json-schema编辑器。git地址:https://github.com/giscafer/vue-json-schema-editor-visualdem......
  • 4.8 C++ Boost 应用JSON解析库
    property_tree是Boost库中的一个头文件库,用于处理和解析基于XML、Json或者INFO格式的数据。property_tree可以提供一个轻量级的、灵活的、基于二叉数的通用容器,可以处理包括简单值(如int、float)和复杂数据结构(如结构体和嵌套容器)在内的各种数据类型。它可以解析数据文件......
  • Java后端使用HttpServleRequest接收前端JSON传参
    BufferedReaderbf=newBufferedReader(newInputStreamReader((ServletInputStream)request.getInputStream(),"utf-8"));StringBuffersb=newStringBuffer();Stringtemp="";while((temp=bf.readLine())!=null){sb.append(temp);}b......
  • JSON字符串中获取一个特定字段的值
    importcom.google.gson.JsonObject;importcom.google.gson.JsonParser;Stringjson="{\"translation\":\"你好吗\"}";//Stringjson="{'translation':'你好吗'}";JsonObjectjsonObject=JsonParser.parseS......
  • JSONObject.toJavaObject
    YgCompanyygCompany=JSONObject.toJavaObject(json,YgCompany.class);这段代码使用了阿里巴巴的fastjson库中的JSONObject.toJavaObject()方法,将JSON字符串转换为Java对象。具体解释如下:1.JSONObject是fastjson库中的一个类,用于处理JSON对象。2.toJavaObject()是JSONObje......