如何在Golang Gin中显示自定义错误并中止

I want to Abort with status and json body if any errors. i don't want to use repeated if condition over and over again.

f, err := os.Create(file)

if(err != nil){
  c.JSON(200, gin.H{"error": true,"message":err.Error() })
  return
}

f, err := os.Create(file)
Error(c,err)

but this function is not aborting

func Error(c *gin.Context,err error) {
  if(err != nil){
  c.JSON(200, gin.H{"status": false,"message":err.Error() })
  c.Error(err)
  c.Abort()
}

Abort, as stated in the documentation, is for aborting the chain of middleware, not to stop the current handler from executing.

The error checking is a feature of Go and you'll just have to do it, the same as everyone else.

However you can simplify your error checking to some extent. For example one solution could be something like this:

func Error(c *gin.Context, err error) bool {
    if err != nil {
        c.Error(err)
        c.AbortWithStatusJSON(200, gin.H{"status": false, "message": err.Error()})
        return true // signal that there was an error and the caller should return
    }
    return false // no error, can continue
}

And then your handler would look like this:

func MyHandler(c *gin.Context) {
    f, err := os.Create(file)
    if Error(c, err) {
        return // exit
    }

    // continue
}

Another approach, and in my mind the more idiomatic one, would be to change the handler to have an error return argument and then have a wrapper that takes these error-returning handlers and returns a valid gin handler, and then this returned gin handler can deal with the error value however you see fit.

// error returning handler
func MyHandler(c *gin.Context) error {
    f, err := os.Create(file)
    if err != nil {
        return err // exit
    }

    // do other stuff...

    return nil // all good
}

// function that turns the error-returning handler into a valid gin handler
func GinHandler(myhandler func(c *gin.Context) error) (ginhandler func(c *gin.Context)) {
    return func(c *gin.Context) {
        if err := myhandler(c); err != nil {
            c.Error(err)
            c.AbortWithStatusJSON(200, gin.H{"status": false, "message": err.Error()})
        }
    }
}

// and you would register the MyHandler like so...
router := gin.Default()
router.GET("/foobar", GinHandler(MyHandler))