当前位置:网站首页>Golang panic recover custom exception handling
Golang panic recover custom exception handling
2022-07-01 06:08:00 【A man like the wind &】
- panic
panic The word , There is panic in English 、 Panic, etc ;panic Keyword is mainly used to actively throw exceptions , similar Python Medium raise keyword
- recover
recover The word , In English, it has the function of restoring 、 Recovery, etc ; Literally , stay Go In language , Represents the restoration of the program state from a serious error to a normal state .Go In language recover The keyword is mainly used to catch exceptions , Return the program to normal , similar Python Medium except, Used to catch exceptions , You can do special operations in specific exception handling .
- Custom Recovery behavior Custom exception capture 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")
}
边栏推荐
- Why use huluer pie disk instead of U disk?
- Record currency in MySQL
- PLA不粘贴在床上:6个简单的解决方案
- OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
- Crossing sect · paipan + Siyuan notes = private notebook
- MySQL怎么存储emoji?
- Oracle sequence + trigger
- Using Baidu map to query national subway lines
- Thoughts on a "01 knapsack problem" expansion problem
- Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
猜你喜欢
excel初级应用案例——杜邦分析仪
Freeswitch dial the extension number
Crossing pie · pie pan + Mountain duck = local data management
论文学习记录随笔 多标签之LIFT
freeswitch拨打分机号
kotlin位运算的坑(bytes[i] and 0xff 报错)
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
PLA not pasted on the bed: 6 simple solutions
Index method and random forest to realize the information of surface water body in wet season in Shandong Province
Essay learning record essay multi label Global
随机推荐
c# Xml帮助类
Infinite horizontal marble game
Differences between in and exists in MySQL
机械臂速成小指南(六):步进电机驱动器
Excel dynamic chart
Code shoe set - mt3114 · interesting balance - explain it with examples
3D printer threading: five simple solutions
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
MySQL怎么存储emoji?
Transformer le village de tiantou en un village de betteraves sucrières
MinIO纠错码、分布式MinIO集群搭建及启动
无限水平大理石游戏
srpingboot security demo
Timer based on LabVIEW
make: g++:命令未找到
MySQL里记录货币
How to transmit and share 4GB large files remotely in real time?
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
表格中el-tooltip 实现换行展示
让厦门灌口镇田头村变“甜头”村的特色农产品之一是