当前位置:网站首页>golang panic recover自定义异常处理
golang panic recover自定义异常处理
2022-07-01 06:05:00 【风一样的男子&】
- panic
panic 这个词,在英语中具有恐慌、恐慌的等意思;panic 关键字主要用于主动抛出异常,类似 Python中的raise关键字
- recover
recover 这个词,在英语中具有恢复、复原等意思;从字面意思理解的话,在 Go 语言中,代表将程序状态从严重的错误中恢复到正常状态。Go语言中 recover 关键字主要用于捕获异常,让程序回到正常状态,类似Python中的except,用来捕获异常,可以在具体异常处理中做特殊操作。
- Custom Recovery behavior 自定义异常捕获行为
func main() {
// Creates a router without any middleware by default
r := gin.New()
// Global middleware
// Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release.
// By default gin.DefaultWriter = os.Stdout
r.Use(gin.Logger())
// Recovery middleware recovers from any panics and writes a 500 if there was one.
r.Use(gin.CustomRecovery(func(c *gin.Context, recovered interface{}) {
if err, ok := recovered.(string); ok {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
}
c.AbortWithStatus(http.StatusInternalServerError)
}))
r.GET("/panic", func(c *gin.Context) {
// panic with a string -- the custom middleware could save this to a database or report it to the user
panic("foo")
})
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "ohai")
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
边栏推荐
- 蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
- In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
- Crossing pie · pie pan + Mountain duck = local data management
- 芯片,建立在沙粒上的帝国!
- Thoughts on a "01 knapsack problem" expansion problem
- SystemVerilog学习-09-进程间同步、通信和虚方法
- Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
- FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
- Don't put your notes and videos everywhere!
- 机械臂速成小指南(六):步进电机驱动器
猜你喜欢
Talking from mlperf: how to lead the next wave of AI accelerator
Database problems, how to optimize Oracle SQL query statements faster and more efficient
FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
【文件系统】如何在ubi之上运行squashfs
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
可动的机械挂钟
Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
Essay learning record essay multi label Global
SystemVerilog学习-10-验证量化和覆盖率
Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
随机推荐
4GB大文件,如何实时远程传输和共享?
Fragment upload and breakpoint resume
Oracle sequence + trigger
2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
Excel dynamic chart
OpenGL ES: (4) EGL API详解 (转)
Thesis learning record essay multi label lift
MySQL数据迁移遇到的一些错误
如何添加葫芦儿派盘
Bat operation FTP upload and download command
68 cesium code datasource loading czml
jdbc 数据库操作
关于一道01背包问题的·拓展题的思考
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
Beauty of Mathematics - Application of Mathematics
MinIO纠错码、分布式MinIO集群搭建及启动
Debug details under pycharm
On the first day of the new year, 3000 Apache servers went down
让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村
芯片,建立在沙粒上的帝国!