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"
)
var CopyTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) {
var CopyTaskManager = task.NewTaskManager(3, func(tid *uint64) {
atomic.AddUint64(tid, 1)
})

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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{

View File

@ -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)

View File

@ -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})

View File

@ -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)

View File

@ -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 {

View File

@ -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()