From d1efec4539bc9e7b4145e7f9352e79f8e95a969a Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Tue, 28 Jun 2022 18:12:53 +0800 Subject: [PATCH] chore: common err resp log --- internal/fs/copy.go | 2 +- server/common/common.go | 13 +++++++++---- server/controllers/account.go | 17 +++++++++-------- server/controllers/down.go | 15 ++++++++------- server/controllers/fsget.go | 7 ++++--- server/controllers/fslist.go | 7 ++++--- server/controllers/login.go | 11 ++++++----- server/controllers/meta.go | 19 ++++++++++--------- server/controllers/user.go | 19 ++++++++++--------- server/middlewares/auth.go | 14 +++++++------- 10 files changed, 68 insertions(+), 56 deletions(-) diff --git a/internal/fs/copy.go b/internal/fs/copy.go index 21dbd604..50fe85a1 100644 --- a/internal/fs/copy.go +++ b/internal/fs/copy.go @@ -15,7 +15,7 @@ import ( "github.com/pkg/errors" ) -var CopyTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) { +var CopyTaskManager = task.NewTaskManager(3, func(tid *uint64) { atomic.AddUint64(tid, 1) }) diff --git a/server/common/common.go b/server/common/common.go index 1bf62475..c436a757 100644 --- a/server/common/common.go +++ b/server/common/common.go @@ -1,15 +1,20 @@ package common import ( + "github.com/alist-org/alist/v3/cmd/args" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" ) // ErrorResp is used to return error response -// @param nl: if true, don't log error -func ErrorResp(c *gin.Context, err error, code int, nl ...bool) { - if len(nl) == 0 || !nl[0] { - log.Errorf("%+v", err) +// @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) + } } c.JSON(200, Resp{ Code: code, diff --git a/server/controllers/account.go b/server/controllers/account.go index 2cc629fa..ee74cd32 100644 --- a/server/controllers/account.go +++ b/server/controllers/account.go @@ -1,19 +1,20 @@ package controllers import ( + "strconv" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" - "strconv" ) func ListAccounts(c *gin.Context) { var req common.PageReq if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } log.Debugf("%+v", req) @@ -31,11 +32,11 @@ func ListAccounts(c *gin.Context) { func CreateAccount(c *gin.Context) { var req model.Account if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } if err := operations.CreateAccount(c, req); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) } else { common.SuccessResp(c) } @@ -44,11 +45,11 @@ func CreateAccount(c *gin.Context) { func UpdateAccount(c *gin.Context) { var req model.Account if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } if err := operations.UpdateAccount(c, req); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) } else { common.SuccessResp(c) } @@ -58,11 +59,11 @@ func DeleteAccount(c *gin.Context) { idStr := c.Query("id") id, err := strconv.Atoi(idStr) if err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } if err := operations.DeleteAccountById(c, uint(id)); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) return } common.SuccessResp(c) diff --git a/server/controllers/down.go b/server/controllers/down.go index 9a6553ef..81afbe19 100644 --- a/server/controllers/down.go +++ b/server/controllers/down.go @@ -1,6 +1,9 @@ package controllers import ( + stdpath "path" + "strings" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/errs" @@ -12,8 +15,6 @@ import ( "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" "github.com/pkg/errors" - stdpath "path" - "strings" ) func Down(c *gin.Context) { @@ -31,13 +32,13 @@ func Down(c *gin.Context) { s := c.Param("sign") err = sign.Verify(filename, s) if err != nil { - common.ErrorResp(c, err, 401, true) + common.ErrorResp(c, err, 401) return } } account, err := fs.GetAccount(rawPath) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } if needProxy(account, filename) { @@ -45,12 +46,12 @@ func Down(c *gin.Context) { Header: c.Request.Header, }) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } obj, err := fs.Get(c, rawPath) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } err = common.Proxy(c.Writer, c.Request, link, obj) @@ -64,7 +65,7 @@ func Down(c *gin.Context) { Header: c.Request.Header, }) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } c.Redirect(302, link.URL) diff --git a/server/controllers/fsget.go b/server/controllers/fsget.go index 6657f169..66a1ccb4 100644 --- a/server/controllers/fsget.go +++ b/server/controllers/fsget.go @@ -1,6 +1,8 @@ package controllers import ( + stdpath "path" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/fs" @@ -8,7 +10,6 @@ import ( "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" "github.com/pkg/errors" - stdpath "path" ) type FsGetReq struct { @@ -32,7 +33,7 @@ func FsGet(c *gin.Context) { meta, err := db.GetNearestMeta(req.Path) if err != nil { if !errors.Is(errors.Cause(err), errs.MetaNotFound) { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } } @@ -43,7 +44,7 @@ func FsGet(c *gin.Context) { } obj, err := fs.Get(c, req.Path) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } common.SuccessResp(c, FsGetResp{ diff --git a/server/controllers/fslist.go b/server/controllers/fslist.go index 05f65120..1d5cc7b3 100644 --- a/server/controllers/fslist.go +++ b/server/controllers/fslist.go @@ -1,6 +1,9 @@ package controllers import ( + stdpath "path" + "time" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/fs" @@ -11,8 +14,6 @@ import ( "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" "github.com/pkg/errors" - stdpath "path" - "time" ) type ListReq struct { @@ -57,7 +58,7 @@ func FsList(c *gin.Context) { } objs, err := fs.List(c, req.Path) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } total, objs := pagination(objs, &req.PageReq) diff --git a/server/controllers/login.go b/server/controllers/login.go index acd8282c..2bd607e2 100644 --- a/server/controllers/login.go +++ b/server/controllers/login.go @@ -1,12 +1,13 @@ package controllers import ( + "time" + "github.com/Xhofe/go-cache" "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" - "time" ) var loginCache = cache.NewMemCache[int]() @@ -32,24 +33,24 @@ func Login(c *gin.Context) { // check username var req LoginReq if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } user, err := db.GetUserByName(req.Username) if err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } // validate password if err := user.ValidatePassword(req.Password); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) loginCache.Set(ip, count+1) return } // generate token token, err := common.GenerateToken(user.Username) if err != nil { - common.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } common.SuccessResp(c, gin.H{"token": token}) diff --git a/server/controllers/meta.go b/server/controllers/meta.go index b5aa4ca9..1192284a 100644 --- a/server/controllers/meta.go +++ b/server/controllers/meta.go @@ -1,25 +1,26 @@ package controllers import ( + "strconv" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" - "strconv" ) func ListMetas(c *gin.Context) { var req common.PageReq if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } log.Debugf("%+v", req) metas, total, err := db.GetMetas(req.PageIndex, req.PageSize) if err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) return } common.SuccessResp(c, common.PageResp{ @@ -31,12 +32,12 @@ func ListMetas(c *gin.Context) { func CreateMeta(c *gin.Context) { var req model.Meta if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } req.Path = utils.StandardizePath(req.Path) if err := db.CreateMeta(&req); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) } else { common.SuccessResp(c) } @@ -45,12 +46,12 @@ func CreateMeta(c *gin.Context) { func UpdateMeta(c *gin.Context) { var req model.Meta if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } req.Path = utils.StandardizePath(req.Path) if err := db.UpdateMeta(&req); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) } else { common.SuccessResp(c) } @@ -60,11 +61,11 @@ func DeleteMeta(c *gin.Context) { idStr := c.Query("id") id, err := strconv.Atoi(idStr) if err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } if err := db.DeleteMetaById(uint(id)); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) return } common.SuccessResp(c) diff --git a/server/controllers/user.go b/server/controllers/user.go index 8d096d64..7ce0a0a0 100644 --- a/server/controllers/user.go +++ b/server/controllers/user.go @@ -1,24 +1,25 @@ package controllers import ( + "strconv" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" - "strconv" ) func ListUsers(c *gin.Context) { var req common.PageReq if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } log.Debugf("%+v", req) users, total, err := db.GetUsers(req.PageIndex, req.PageSize) if err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) return } common.SuccessResp(c, common.PageResp{ @@ -30,7 +31,7 @@ func ListUsers(c *gin.Context) { func CreateUser(c *gin.Context) { var req model.User if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } if req.IsAdmin() || req.IsGuest() { @@ -38,7 +39,7 @@ func CreateUser(c *gin.Context) { return } if err := db.CreateUser(&req); err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) } else { common.SuccessResp(c) } @@ -47,16 +48,16 @@ func CreateUser(c *gin.Context) { func UpdateUser(c *gin.Context) { var req model.User if err := c.ShouldBind(&req); err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } user, err := db.GetUserById(req.ID) if err != nil { - common.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } if user.Role != req.Role { - common.ErrorStrResp(c, "role can not be changed", 400, true) + common.ErrorStrResp(c, "role can not be changed", 400) return } if err := db.UpdateUser(&req); err != nil { @@ -70,7 +71,7 @@ func DeleteUser(c *gin.Context) { idStr := c.Query("id") id, err := strconv.Atoi(idStr) if err != nil { - common.ErrorResp(c, err, 400, true) + common.ErrorResp(c, err, 400) return } if err := db.DeleteUserById(uint(id)); err != nil { diff --git a/server/middlewares/auth.go b/server/middlewares/auth.go index 556ed940..d458998e 100644 --- a/server/middlewares/auth.go +++ b/server/middlewares/auth.go @@ -4,7 +4,7 @@ import ( "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/setting" - common2 "github.com/alist-org/alist/v3/server/common" + "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" ) @@ -15,7 +15,7 @@ func Auth(c *gin.Context) { if token == setting.GetByKey("token") { admin, err := db.GetAdmin() if err != nil { - common2.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500) c.Abort() return } @@ -26,7 +26,7 @@ func Auth(c *gin.Context) { if token == "" { guest, err := db.GetGuest() if err != nil { - common2.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500) c.Abort() return } @@ -34,15 +34,15 @@ func Auth(c *gin.Context) { c.Next() return } - userClaims, err := common2.ParseToken(token) + userClaims, err := common.ParseToken(token) if err != nil { - common2.ErrorResp(c, err, 401, true) + common.ErrorResp(c, err, 401) c.Abort() return } user, err := db.GetUserByName(userClaims.Username) if err != nil { - common2.ErrorResp(c, err, 401) + common.ErrorResp(c, err, 401) c.Abort() return } @@ -53,7 +53,7 @@ func Auth(c *gin.Context) { func AuthAdmin(c *gin.Context) { user := c.MustGet("user").(*model.User) if !user.IsAdmin() { - common2.ErrorStrResp(c, "You are not an admin", 403, true) + common.ErrorStrResp(c, "You are not an admin", 403) c.Abort() } else { c.Next()