alist/server/common/common.go

54 lines
948 B
Go
Raw Normal View History

2022-06-25 13:34:44 +00:00
package common
import (
2022-06-28 10:12:53 +00:00
"github.com/alist-org/alist/v3/cmd/args"
2022-06-25 13:34:44 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
2022-06-26 11:20:19 +00:00
// ErrorResp is used to return error response
2022-06-28 10:12:53 +00:00
// @param l: if true, log error
func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
if len(l) > 0 && l[0] {
if args.Debug || args.Dev {
log.Errorf("%+v", err)
} else {
log.Errorf("%v", err)
}
2022-06-25 14:05:02 +00:00
}
2022-06-25 13:34:44 +00:00
c.JSON(200, Resp{
Code: code,
Message: err.Error(),
Data: nil,
})
c.Abort()
}
2022-06-26 11:20:19 +00:00
func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) {
if len(l) != 0 && l[0] {
log.Error(str)
}
2022-06-25 13:34:44 +00:00
c.JSON(200, Resp{
Code: code,
Message: str,
Data: nil,
})
c.Abort()
}
func SuccessResp(c *gin.Context, data ...interface{}) {
if len(data) == 0 {
c.JSON(200, Resp{
Code: 200,
Message: "success",
Data: nil,
})
return
}
c.JSON(200, Resp{
Code: 200,
Message: "success",
Data: data[0],
})
}