当前位置:网站首页>gin abort不能阻止后续代码的问题
gin abort不能阻止后续代码的问题
2022-07-29 07:07:00 【卖烤冷面的郭师傅】
在校验参数时发现参数错误和正确的json一起输出了出来导致前端解析错误
代码如下:
//utils.go
//返回json字符串
func ReturnJson(code int16, msg string, data interface{
}, c *gin.Context) {
c.JSON(200, gin.H{
"code": code,
"msg": msg,
"data": data,
})
}
//bce.go
//翻译
func Translate(c *gin.Context) {
type TranslateJson struct {
TplId int `json:"tplId"`
Lang string `json:"lang"`
All int `json:"All"`
}
translate := TranslateJson{
}
err := c.BindJSON(&translate)
if err != nil {
panic(err)
}
if templateLangConfig[translate.Lang] == nil {
utils.ReturnJson(-21003, "未找到该语种", nil, c)
}
utils.ReturnJson(0, "ok", nil, c)
}
以上同时输出了 {“code”:-21003,“data”:null,“msg”:“未找到该语种”}{“code”:0,“data”:null,“msg”:“ok”}导致前端解析失败了
后边百度说是需要调用gin.context.Abort() ,但是我加上后并没有作用,而且打印c.IsAborted()为true仍然继续执行。。。
来看下官方介绍:
//Abort prevents pending handlers from being called.
//Note that this will not stop the current handler.
//Let's say you have an authorization middleware that validates that the current request is authorized.
//If the authorization fails (ex: the password does not match),
//call Abort to ensure the remaining handlers for this request are not called.
func (c *Context)Abort()
意思大概是说abort只是阻止挂起函数,并不会停止当前的函数,那么其实就很简单了,只要在utils.returnJson后加上return即可。。。
if templateLangConfig[translate.Lang] == nil {
utils.ReturnJson(-21003, "未找到该语种", nil, c)
return
}
边栏推荐
- logback中RollingFileAppender属性简介说明
- WPF nested layout case
- WPF interface layout must know basis
- After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
- Excel file reading and writing (creation and parsing)
- Error 1045 (28000) access denied for user 'root' @ 'localhost' solution
- logback filter过滤器简介说明
- 7-2 计算正五边形的面积和周长 (25分)
- Tp6 use protobuf
- Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
猜你喜欢

1-后台项目搭建

MySQL 高级(进阶) SQL 语句 (一)

Redis Basics

My personal website doesn't allow access to wechat, so I did this

Docker最新超详细教程——Docker创建运行MySQL并挂载
Scala higher order (IX): pattern matching in Scala

对Vintage分析的一些学习理解
Scala 高阶(九):Scala中的模式匹配

JS day 4 process control (if statement and switch statement)

Leetcode 879. profit plan
随机推荐
Vite3.0都发布了,你还能卷得动吗(新特性一览)
ETL为什么经常变成ELT甚至LET?
route的meta配置项
计算程序运行时间 demo
How to establish EDI connection with Scania in Scania?
tp6 使用 ProtoBuf
Gin Middleware
logback日志级别简介说明
log4j Layout简介说明
How much data can a single MySQL table store at most?
Docker最新超详细教程——Docker创建运行MySQL并挂载
Vue router route cache
Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
[OpenGL] use of shaders
NPM install reports an error NPM err could not resolve dependency NPM err peer
PAT甲级 1150 旅行商问题
QT topic: basic components (button class, layout class, output class, input class, container class)
3-全局异常处理
Leetcode 879. profit plan
logback appender简介说明