func handleRequest(c *gin.Context) { if err := someFunction(); err != nil { c.Fail(http.StatusInternalServerError, err.Error()) return } // Continue with request handling }
func handleRequest(c *gin.Context) { user := getUser() if user == nil { c.Fail(http.StatusUnauthorized, "Unauthorized") return } // Continue with request handling }Here, if there is no authenticated user, the `Fail` method is called with a `401 Unauthorized` status code and the message "Unauthorized" as the response body.