chore: common err resp log

refactor/fs
Noah Hsu 2022-06-28 18:12:53 +08:00
parent 67bc66fedf
commit d1efec4539
10 changed files with 68 additions and 56 deletions

View File

@ -15,7 +15,7 @@ import (
"github.com/pkg/errors" "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) atomic.AddUint64(tid, 1)
}) })

View File

@ -1,15 +1,20 @@
package common package common
import ( import (
"github.com/alist-org/alist/v3/cmd/args"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// ErrorResp is used to return error response // ErrorResp is used to return error response
// @param nl: if true, don't log error // @param l: if true, log error
func ErrorResp(c *gin.Context, err error, code int, nl ...bool) { func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
if len(nl) == 0 || !nl[0] { if len(l) > 0 && l[0] {
log.Errorf("%+v", err) if args.Debug || args.Dev {
log.Errorf("%+v", err)
} else {
log.Errorf("%v", err)
}
} }
c.JSON(200, Resp{ c.JSON(200, Resp{
Code: code, Code: code,

View File

@ -1,19 +1,20 @@
package controllers package controllers
import ( import (
"strconv"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv"
) )
func ListAccounts(c *gin.Context) { func ListAccounts(c *gin.Context) {
var req common.PageReq var req common.PageReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
log.Debugf("%+v", req) log.Debugf("%+v", req)
@ -31,11 +32,11 @@ func ListAccounts(c *gin.Context) {
func CreateAccount(c *gin.Context) { func CreateAccount(c *gin.Context) {
var req model.Account var req model.Account
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
if err := operations.CreateAccount(c, req); err != nil { if err := operations.CreateAccount(c, req); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
} else { } else {
common.SuccessResp(c) common.SuccessResp(c)
} }
@ -44,11 +45,11 @@ func CreateAccount(c *gin.Context) {
func UpdateAccount(c *gin.Context) { func UpdateAccount(c *gin.Context) {
var req model.Account var req model.Account
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
if err := operations.UpdateAccount(c, req); err != nil { if err := operations.UpdateAccount(c, req); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
} else { } else {
common.SuccessResp(c) common.SuccessResp(c)
} }
@ -58,11 +59,11 @@ func DeleteAccount(c *gin.Context) {
idStr := c.Query("id") idStr := c.Query("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
if err := operations.DeleteAccountById(c, uint(id)); err != nil { if err := operations.DeleteAccountById(c, uint(id)); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
return return
} }
common.SuccessResp(c) common.SuccessResp(c)

View File

@ -1,6 +1,9 @@
package controllers package controllers
import ( import (
stdpath "path"
"strings"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/errs"
@ -12,8 +15,6 @@ import (
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
stdpath "path"
"strings"
) )
func Down(c *gin.Context) { func Down(c *gin.Context) {
@ -31,13 +32,13 @@ func Down(c *gin.Context) {
s := c.Param("sign") s := c.Param("sign")
err = sign.Verify(filename, s) err = sign.Verify(filename, s)
if err != nil { if err != nil {
common.ErrorResp(c, err, 401, true) common.ErrorResp(c, err, 401)
return return
} }
} }
account, err := fs.GetAccount(rawPath) account, err := fs.GetAccount(rawPath)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
if needProxy(account, filename) { if needProxy(account, filename) {
@ -45,12 +46,12 @@ func Down(c *gin.Context) {
Header: c.Request.Header, Header: c.Request.Header,
}) })
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
obj, err := fs.Get(c, rawPath) obj, err := fs.Get(c, rawPath)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
err = common.Proxy(c.Writer, c.Request, link, obj) err = common.Proxy(c.Writer, c.Request, link, obj)
@ -64,7 +65,7 @@ func Down(c *gin.Context) {
Header: c.Request.Header, Header: c.Request.Header,
}) })
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
c.Redirect(302, link.URL) c.Redirect(302, link.URL)

View File

@ -1,6 +1,8 @@
package controllers package controllers
import ( import (
stdpath "path"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs" "github.com/alist-org/alist/v3/internal/fs"
@ -8,7 +10,6 @@ import (
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
stdpath "path"
) )
type FsGetReq struct { type FsGetReq struct {
@ -32,7 +33,7 @@ func FsGet(c *gin.Context) {
meta, err := db.GetNearestMeta(req.Path) meta, err := db.GetNearestMeta(req.Path)
if err != nil { if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) { if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
} }
@ -43,7 +44,7 @@ func FsGet(c *gin.Context) {
} }
obj, err := fs.Get(c, req.Path) obj, err := fs.Get(c, req.Path)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
common.SuccessResp(c, FsGetResp{ common.SuccessResp(c, FsGetResp{

View File

@ -1,6 +1,9 @@
package controllers package controllers
import ( import (
stdpath "path"
"time"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs" "github.com/alist-org/alist/v3/internal/fs"
@ -11,8 +14,6 @@ import (
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
stdpath "path"
"time"
) )
type ListReq struct { type ListReq struct {
@ -57,7 +58,7 @@ func FsList(c *gin.Context) {
} }
objs, err := fs.List(c, req.Path) objs, err := fs.List(c, req.Path)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
total, objs := pagination(objs, &req.PageReq) total, objs := pagination(objs, &req.PageReq)

View File

@ -1,12 +1,13 @@
package controllers package controllers
import ( import (
"time"
"github.com/Xhofe/go-cache" "github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"time"
) )
var loginCache = cache.NewMemCache[int]() var loginCache = cache.NewMemCache[int]()
@ -32,24 +33,24 @@ func Login(c *gin.Context) {
// check username // check username
var req LoginReq var req LoginReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
user, err := db.GetUserByName(req.Username) user, err := db.GetUserByName(req.Username)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
// validate password // validate password
if err := user.ValidatePassword(req.Password); err != nil { if err := user.ValidatePassword(req.Password); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
loginCache.Set(ip, count+1) loginCache.Set(ip, count+1)
return return
} }
// generate token // generate token
token, err := common.GenerateToken(user.Username) token, err := common.GenerateToken(user.Username)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
common.SuccessResp(c, gin.H{"token": token}) common.SuccessResp(c, gin.H{"token": token})

View File

@ -1,25 +1,26 @@
package controllers package controllers
import ( import (
"strconv"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv"
) )
func ListMetas(c *gin.Context) { func ListMetas(c *gin.Context) {
var req common.PageReq var req common.PageReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
log.Debugf("%+v", req) log.Debugf("%+v", req)
metas, total, err := db.GetMetas(req.PageIndex, req.PageSize) metas, total, err := db.GetMetas(req.PageIndex, req.PageSize)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
return return
} }
common.SuccessResp(c, common.PageResp{ common.SuccessResp(c, common.PageResp{
@ -31,12 +32,12 @@ func ListMetas(c *gin.Context) {
func CreateMeta(c *gin.Context) { func CreateMeta(c *gin.Context) {
var req model.Meta var req model.Meta
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
req.Path = utils.StandardizePath(req.Path) req.Path = utils.StandardizePath(req.Path)
if err := db.CreateMeta(&req); err != nil { if err := db.CreateMeta(&req); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
} else { } else {
common.SuccessResp(c) common.SuccessResp(c)
} }
@ -45,12 +46,12 @@ func CreateMeta(c *gin.Context) {
func UpdateMeta(c *gin.Context) { func UpdateMeta(c *gin.Context) {
var req model.Meta var req model.Meta
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
req.Path = utils.StandardizePath(req.Path) req.Path = utils.StandardizePath(req.Path)
if err := db.UpdateMeta(&req); err != nil { if err := db.UpdateMeta(&req); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
} else { } else {
common.SuccessResp(c) common.SuccessResp(c)
} }
@ -60,11 +61,11 @@ func DeleteMeta(c *gin.Context) {
idStr := c.Query("id") idStr := c.Query("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
if err := db.DeleteMetaById(uint(id)); err != nil { if err := db.DeleteMetaById(uint(id)); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
return return
} }
common.SuccessResp(c) common.SuccessResp(c)

View File

@ -1,24 +1,25 @@
package controllers package controllers
import ( import (
"strconv"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv"
) )
func ListUsers(c *gin.Context) { func ListUsers(c *gin.Context) {
var req common.PageReq var req common.PageReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
log.Debugf("%+v", req) log.Debugf("%+v", req)
users, total, err := db.GetUsers(req.PageIndex, req.PageSize) users, total, err := db.GetUsers(req.PageIndex, req.PageSize)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
return return
} }
common.SuccessResp(c, common.PageResp{ common.SuccessResp(c, common.PageResp{
@ -30,7 +31,7 @@ func ListUsers(c *gin.Context) {
func CreateUser(c *gin.Context) { func CreateUser(c *gin.Context) {
var req model.User var req model.User
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
if req.IsAdmin() || req.IsGuest() { if req.IsAdmin() || req.IsGuest() {
@ -38,7 +39,7 @@ func CreateUser(c *gin.Context) {
return return
} }
if err := db.CreateUser(&req); err != nil { if err := db.CreateUser(&req); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500, true)
} else { } else {
common.SuccessResp(c) common.SuccessResp(c)
} }
@ -47,16 +48,16 @@ func CreateUser(c *gin.Context) {
func UpdateUser(c *gin.Context) { func UpdateUser(c *gin.Context) {
var req model.User var req model.User
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
user, err := db.GetUserById(req.ID) user, err := db.GetUserById(req.ID)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
if user.Role != req.Role { 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 return
} }
if err := db.UpdateUser(&req); err != nil { if err := db.UpdateUser(&req); err != nil {
@ -70,7 +71,7 @@ func DeleteUser(c *gin.Context) {
idStr := c.Query("id") idStr := c.Query("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400, true) common.ErrorResp(c, err, 400)
return return
} }
if err := db.DeleteUserById(uint(id)); err != nil { if err := db.DeleteUserById(uint(id)); err != nil {

View File

@ -4,7 +4,7 @@ import (
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/setting" "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" "github.com/gin-gonic/gin"
) )
@ -15,7 +15,7 @@ func Auth(c *gin.Context) {
if token == setting.GetByKey("token") { if token == setting.GetByKey("token") {
admin, err := db.GetAdmin() admin, err := db.GetAdmin()
if err != nil { if err != nil {
common2.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
c.Abort() c.Abort()
return return
} }
@ -26,7 +26,7 @@ func Auth(c *gin.Context) {
if token == "" { if token == "" {
guest, err := db.GetGuest() guest, err := db.GetGuest()
if err != nil { if err != nil {
common2.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
c.Abort() c.Abort()
return return
} }
@ -34,15 +34,15 @@ func Auth(c *gin.Context) {
c.Next() c.Next()
return return
} }
userClaims, err := common2.ParseToken(token) userClaims, err := common.ParseToken(token)
if err != nil { if err != nil {
common2.ErrorResp(c, err, 401, true) common.ErrorResp(c, err, 401)
c.Abort() c.Abort()
return return
} }
user, err := db.GetUserByName(userClaims.Username) user, err := db.GetUserByName(userClaims.Username)
if err != nil { if err != nil {
common2.ErrorResp(c, err, 401) common.ErrorResp(c, err, 401)
c.Abort() c.Abort()
return return
} }
@ -53,7 +53,7 @@ func Auth(c *gin.Context) {
func AuthAdmin(c *gin.Context) { func AuthAdmin(c *gin.Context) {
user := c.MustGet("user").(*model.User) user := c.MustGet("user").(*model.User)
if !user.IsAdmin() { if !user.IsAdmin() {
common2.ErrorStrResp(c, "You are not an admin", 403, true) common.ErrorStrResp(c, "You are not an admin", 403)
c.Abort() c.Abort()
} else { } else {
c.Next() c.Next()