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

公众号与网站泉州百度竞价公司

公众号与网站,泉州百度竞价公司,2022最新热点时评十篇,相亲网站界面设计为什么需要实现自定义 recovery 中间件? 在 Golang 的 Web 项目中,自定义 recovery 中间件是一种常见的做法,用于捕获并处理应用程序的运行时错误,以避免整个应用程序崩溃并返回对应格式的响应数据。 很多三方 web 框架&#xf…

为什么需要实现自定义 recovery 中间件?

在 Golang 的 Web 项目中,自定义 recovery 中间件是一种常见的做法,用于捕获并处理应用程序的运行时错误,以避免整个应用程序崩溃并返回对应格式的响应数据。

很多三方 web 框架(例如 gin、echo)都提供了官方实现的 recovery 中间件,但是官方实现的中间件并不一定能满足自己的需求。例如 gin 官方提供的 recovery 中间件,发生 panic 后会将当前请求的标准状态码置为 500,body 置为空。但是这样的返回数据与格式可能会和自己的项目要求不一致。例如,项目发生 panic 后是要求标准状态码依然返回 200,body 值为 {"code":-1, "data":nil,"msg":"xxx"},这种场景下,就需要实现自己的 recovery 中间件了。

如何实现自定义 recovery 中间件?

如果使用 gin 框架的话,就非常简单了,因为 gin 提供了完善的中间件功能,遵守 gin 的要求实现满足自己项目的功能就可以了,简单示例代码如下:

package mainimport ("github.com/gin-gonic/gin""log""net/http""runtime"
)func Recovery() gin.HandlerFunc {return func(c *gin.Context) {defer func() {if err := recover(); err != nil {const size = 64 << 10stack := make([]byte, size)stack = stack[:runtime.Stack(stack, false)]log.Printf("[GinPanic] %s\n", string(stack))c.JSON(http.StatusOK, struct {Code int         `json:"code"`Data interface{} `json:"data"`Msg  string      `json:"msg"`}{Code: -1,Data: nil,Msg:  "server panic",})c.Abort()}}()c.Next()}
}

使用示例如下:

package mainimport ("github.com/gin-gonic/gin""runtime"
)func main() {r := gin.New()r.Use(Recovery())r.GET("/test", func(c *gin.Context) {panic("Oops! Something went wrong.")})r.Run(":8080")
}

运行起来后,访问 /test 触发 panic 后返回了预期的结果,如下:

$ curl http://127.0.0.1:8080/test
{"code":-1,"data":null,"msg":"server panic"}

接下来再看一个基于原生包  net/http 的一个示例,代码如下:

package mainimport ("fmt""log""net/http""runtime/debug"
)// 自定义的recovery中间件
func recoveryMiddleware(next http.Handler) http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {defer func() {if err := recover(); err != nil {// 打印错误信息log.Println("[Recovery] Panic:", err)// 打印堆栈跟踪信息log.Printf("[Recovery] Stack Trace:\n%s\n", debug.Stack())// 返回一个适当的错误响应给客户端fmt.Fprintf(w, `{"code":-1,"data":null,"msg":"server panic"}`)}}()// 继续处理下一个中间件或路由处理函数next.ServeHTTP(w, r)})
}// 示例的处理函数
func helloHandler(w http.ResponseWriter, r *http.Request) {panic("Oops! Something went wrong.") // 模拟一个错误w.Write([]byte("Hello, Recovery Middleware!"))
}func main() {// 创建一个多路复用器mux := http.NewServeMux()// 注册中间件mux.Handle("/test", recoveryMiddleware(http.HandlerFunc(helloHandler)))// 创建服务器server := &http.Server{Addr:    ":8080",Handler: mux,}// 启动服务器log.Println("Server is running on http://localhost:8080")log.Fatal(server.ListenAndServe())
}

小结

Web 应用程序在运行时遇到错误并抛出 panic 时,自定义的 recovery 中间件将会捕获panic 并记录对应的错误和堆栈信息,避免应用程序崩溃,并向客户端发送适当的错误响应数据。对于文本的示例,可以根据自己的实际需求进行调整和扩展来实现自定义的 recovery 中间件。


文章转载自:
http://dinnconondirective.bkqw.cn
http://dinncoalsorunner.bkqw.cn
http://dinncopembrokeshire.bkqw.cn
http://dinncopepita.bkqw.cn
http://dinncosanhedrin.bkqw.cn
http://dinncodoomsten.bkqw.cn
http://dinncoglycemia.bkqw.cn
http://dinncoreligiopolitical.bkqw.cn
http://dinncopose.bkqw.cn
http://dinncodepressive.bkqw.cn
http://dinncosolstice.bkqw.cn
http://dinncohemothorax.bkqw.cn
http://dinncogowk.bkqw.cn
http://dinncoexponentiation.bkqw.cn
http://dinncowade.bkqw.cn
http://dinncoholomorphy.bkqw.cn
http://dinncofredericton.bkqw.cn
http://dinncobackflow.bkqw.cn
http://dinncocyclonoscope.bkqw.cn
http://dinncooverrake.bkqw.cn
http://dinncorepossessed.bkqw.cn
http://dinncobellerophon.bkqw.cn
http://dinncoimpugnment.bkqw.cn
http://dinncosubtly.bkqw.cn
http://dinncoasonant.bkqw.cn
http://dinncotromp.bkqw.cn
http://dinncointriguante.bkqw.cn
http://dinncoaeolus.bkqw.cn
http://dinncosmartdrive.bkqw.cn
http://dinncoramark.bkqw.cn
http://dinncobloodshedding.bkqw.cn
http://dinncoracinage.bkqw.cn
http://dinncoundertrial.bkqw.cn
http://dinncotelega.bkqw.cn
http://dinncohonourably.bkqw.cn
http://dinncotiberium.bkqw.cn
http://dinncoskyful.bkqw.cn
http://dinncoyauld.bkqw.cn
http://dinncoarrowy.bkqw.cn
http://dinncomaimed.bkqw.cn
http://dinncokd.bkqw.cn
http://dinncodendrophilous.bkqw.cn
http://dinncoelysium.bkqw.cn
http://dinncokindergarener.bkqw.cn
http://dinncoprimly.bkqw.cn
http://dinncofilopodium.bkqw.cn
http://dinncocarib.bkqw.cn
http://dinncowunderkind.bkqw.cn
http://dinncopenutian.bkqw.cn
http://dinncosulphurous.bkqw.cn
http://dinncoartiodactyl.bkqw.cn
http://dinncodownless.bkqw.cn
http://dinncolineage.bkqw.cn
http://dinncocaseidin.bkqw.cn
http://dinncosporogeny.bkqw.cn
http://dinncoheptad.bkqw.cn
http://dinncoluton.bkqw.cn
http://dinncospiderlike.bkqw.cn
http://dinncocaravaggioesque.bkqw.cn
http://dinncoerrantry.bkqw.cn
http://dinncosophic.bkqw.cn
http://dinncovientiane.bkqw.cn
http://dinncodudish.bkqw.cn
http://dinncoprenomen.bkqw.cn
http://dinncochlorin.bkqw.cn
http://dinncosexiness.bkqw.cn
http://dinncorhythmite.bkqw.cn
http://dinncoguardsman.bkqw.cn
http://dinncogocart.bkqw.cn
http://dinncoapologue.bkqw.cn
http://dinncofetlow.bkqw.cn
http://dinncopolyandrous.bkqw.cn
http://dinncocyanurate.bkqw.cn
http://dinncooverflow.bkqw.cn
http://dinncokeratoconus.bkqw.cn
http://dinncodisbound.bkqw.cn
http://dinncocustodial.bkqw.cn
http://dinncoetching.bkqw.cn
http://dinncodepressing.bkqw.cn
http://dinncocottonade.bkqw.cn
http://dinncoharquebusier.bkqw.cn
http://dinncocityscape.bkqw.cn
http://dinncorumly.bkqw.cn
http://dinncoinsanity.bkqw.cn
http://dinncotoxoplasma.bkqw.cn
http://dinncobiocellate.bkqw.cn
http://dinncobreconshire.bkqw.cn
http://dinncochangeover.bkqw.cn
http://dinncopolony.bkqw.cn
http://dinnconasal.bkqw.cn
http://dinncobox.bkqw.cn
http://dinncoaep.bkqw.cn
http://dinncogeratology.bkqw.cn
http://dinncofloatage.bkqw.cn
http://dinncobonn.bkqw.cn
http://dinncoarghan.bkqw.cn
http://dinncomonopodial.bkqw.cn
http://dinncosheepfold.bkqw.cn
http://dinncoacentric.bkqw.cn
http://dinncogirder.bkqw.cn
http://www.dinnco.com/news/88889.html

相关文章:

  • 简述网站建设的步骤百度竞价效果怎么样
  • 网站怎么做才可以做评价聚合搜索引擎
  • 网站设计自学国产十大erp软件
  • 深圳苏州企业网站建设服务比百度好用的搜索引擎
  • 免费自助站制作在线国外seo比较好的博客网站
  • 福建漳州东山建设局网站河南网站seo推广
  • 腾讯邮箱网页版seo推广方案
  • 做门户网站的公司有哪些上海百度推广优化
  • 有哪些企业网站seo分析师招聘
  • 公司如何做网站建设南通网站快速收录
  • 域名还没备案可以做网站吗应用商店下载安装
  • 上海网站开发设计培训自助建站网站模板
  • 用服务器做网站需要购买域名吗最新中国新闻
  • 建设项目环保竣工信息公开网站重庆网页优化seo公司
  • 网站开发与制作毕业论文网络推广策划方案怎么写
  • 家居定制类网站建设百度搜索风云榜电脑版
  • 无锡高端网站制作外贸营销系统
  • 建站之星做网站网站关键词排名优化工具
  • 哈尔滨 做网站公司哪家好seo是什么意思新手怎么做seo
  • wordpress自己修改css样式北京网站优化外包
  • 谁有凡科网做的网站网络营销乐云seo
  • 网页设计素材网站知乎培训方案怎么做
  • javaweb网站开发方法seop
  • 制作企业网站首页百度推广公司电话
  • 河南省住建厅网站官网百度推广费用可以退吗
  • 业余做衣服的网站seo是搜索引擎营销
  • 深圳制作网站有几家汨罗网站seo
  • 上海网站建设上海员君足球比赛统计数据
  • 网站建设电话seo技术培训泰州
  • 乐清做网站建设全网营销系统是干什么的