当前位置:网站首页>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")
}边栏推荐
- kubeadm搭建kubenetes 集群(个人学习版)
- Using Baidu map to query national subway lines
- srpingboot security demo
- π disk, turning your computer into a personal private cloud
- 分布式锁实现
- linux 关闭redis 进程 systemd+
- OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
- Record currency in MySQL
- FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
- 2022 the 8th China International "Internet +" college student innovation and entrepreneurship competition industry proposition track is open for registration!
猜你喜欢

论文学习记录随笔 多标签之LIFT

FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源

Huluer app help

Index method and random forest to realize the information of surface water body in wet season in Shandong Province

3D printer threading: five simple solutions

Infinite horizontal marble game

skywalking集成nacos动态配置

68 cesium code datasource loading czml

How to add a gourd pie plate

Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
随机推荐
让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村
c# Xml帮助类
Skywalking integrated Nacos dynamic configuration
My experience from technology to product manager
3D打印机穿线:5种简单的解决方案
Database problems, how to optimize Oracle SQL query statements faster and more efficient
Primary application case of Excel DuPont analyzer
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
Fragment upload and breakpoint resume
Talking from mlperf: how to lead the next wave of AI accelerator
Essay learning record essay multi label Global
Multi label lsml for essay learning records
MySQL中 in 和 exists 的区别
阶乘约数(唯一分解定理)
Fixed height of the first column in El table dynamic header rendering
Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
Linux closes the redis process SYSTEMd+
TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
分布式锁实现
LED lighting used in health lighting