From 08104646ba12715669051377876b004e64e29e25 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 20 Jul 2022 19:59:13 +0800 Subject: [PATCH] i18n: error codes for aria2 / callback/ directory operation --- assets | 2 +- pkg/filesystem/errors.go | 31 +++++++++++++++---------------- pkg/serializer/error.go | 25 ++++++++++++++++++++++++- routers/controllers/callback.go | 2 +- service/admin/file.go | 4 ++-- service/aria2/add.go | 22 +++++++++++----------- service/aria2/manage.go | 14 +++++++------- service/callback/upload.go | 24 ++++++++++++------------ service/explorer/directory.go | 4 ++-- service/explorer/file.go | 2 +- 10 files changed, 76 insertions(+), 54 deletions(-) diff --git a/assets b/assets index f47af47..a1028e7 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit f47af47efeecae015dd28a76ab797665391029e0 +Subproject commit a1028e7e0ae96be4bb67d8c117cf39e07c207473 diff --git a/pkg/filesystem/errors.go b/pkg/filesystem/errors.go index 63d1961..6ca9e3a 100644 --- a/pkg/filesystem/errors.go +++ b/pkg/filesystem/errors.go @@ -7,20 +7,19 @@ import ( ) var ( - ErrUnknownPolicyType = errors.New("未知存储策略类型") - ErrFileSizeTooBig = errors.New("单个文件尺寸太大") - ErrFileExtensionNotAllowed = errors.New("不允许上传此类型的文件") - ErrInsufficientCapacity = errors.New("容量空间不足") - ErrIllegalObjectName = errors.New("目标名称非法") - ErrClientCanceled = errors.New("客户端取消操作") - ErrRootProtected = errors.New("无法对根目录进行操作") - ErrInsertFileRecord = serializer.NewError(serializer.CodeDBError, "无法插入文件记录", nil) - ErrFileExisted = serializer.NewError(serializer.CodeObjectExist, "同名文件或目录已存在", nil) - ErrFileUploadSessionExisted = serializer.NewError(serializer.CodeObjectExist, "当前目录下已经有同名文件正在上传中,请尝试清空上传会话", nil) - ErrFolderExisted = serializer.NewError(serializer.CodeObjectExist, "同名目录已存在", nil) - ErrPathNotExist = serializer.NewError(404, "路径不存在", nil) - ErrObjectNotExist = serializer.NewError(404, "文件不存在", nil) - ErrIO = serializer.NewError(serializer.CodeIOFailed, "无法读取文件数据", nil) - ErrDBListObjects = serializer.NewError(serializer.CodeDBError, "无法列取对象记录", nil) - ErrDBDeleteObjects = serializer.NewError(serializer.CodeDBError, "无法删除对象记录", nil) + ErrUnknownPolicyType = serializer.NewError(serializer.CodeInternalSetting, "Unknown policy type", nil) + ErrFileSizeTooBig = serializer.NewError(serializer.CodeFileTooLarge, "", nil) + ErrFileExtensionNotAllowed = serializer.NewError(serializer.CodeFileTypeNotAllowed, "", nil) + ErrInsufficientCapacity = serializer.NewError(serializer.CodeInsufficientCapacity, "", nil) + ErrIllegalObjectName = serializer.NewError(serializer.CodeIllegalObjectName, "", nil) + ErrClientCanceled = errors.New("Client canceled operation") + ErrRootProtected = serializer.NewError(serializer.CodeRootProtected, "", nil) + ErrInsertFileRecord = serializer.NewError(serializer.CodeDBError, "Failed to create file record", nil) + ErrFileExisted = serializer.NewError(serializer.CodeObjectExist, "", nil) + ErrFileUploadSessionExisted = serializer.NewError(serializer.CodeConflictUploadOngoing, "", nil) + ErrPathNotExist = serializer.NewError(serializer.CodeParentNotExist, "", nil) + ErrObjectNotExist = serializer.NewError(serializer.CodeParentNotExist, "", nil) + ErrIO = serializer.NewError(serializer.CodeIOFailed, "Failed to read file data", nil) + ErrDBListObjects = serializer.NewError(serializer.CodeDBError, "Failed to list object records", nil) + ErrDBDeleteObjects = serializer.NewError(serializer.CodeDBError, "Failed to delete object records", nil) ) diff --git a/pkg/serializer/error.go b/pkg/serializer/error.go index 2b31aa2..19f520c 100644 --- a/pkg/serializer/error.go +++ b/pkg/serializer/error.go @@ -142,6 +142,24 @@ const ( CodeListFilesError = 40045 // 对系统节点进行非法操作 CodeInvalidActionOnSystemNode = 40046 + // 创建文件系统出错 + CodeCreateFSError = 40047 + // 创建任务出错 + CodeCreateTaskError = 40048 + // 文件尺寸太大 + CodeFileTooLarge = 40049 + // 文件类型不允许 + CodeFileTypeNotAllowed = 40050 + // 用户容量不足 + CodeInsufficientCapacity = 40051 + // 对象名非法 + CodeIllegalObjectName = 40052 + // 不支持对根目录执行此操作 + CodeRootProtected = 40053 + // 当前目录下已经有同名文件正在上传中 + CodeConflictUploadOngoing = 40054 + // 文件信息不一致 + CodeMetaMismatch = 40055 // CodeDBError 数据库操作失败 CodeDBError = 50001 // CodeEncryptError 加密失败 @@ -158,6 +176,10 @@ const ( CodeUpdateSetting = 50008 // 跨域策略添加失败 CodeAddCORS = 50009 + // 节点不可用 + CodeNodeOffline = 50010 + // 文件元信息查询失败 + CodeQueryMetaFailed = 50011 //CodeParamErr 各种奇奇怪怪的参数错误 CodeParamErr = 40001 // CodeNotSet 未定错误,后续尝试从error中获取 @@ -183,7 +205,8 @@ func ParamErr(msg string, err error) Response { // Err 通用错误处理 func Err(errCode int, msg string, err error) Response { // 底层错误是AppError,则尝试从AppError中获取详细信息 - if appError, ok := err.(AppError); ok { + var appError AppError + if errors.As(err, &appError) { errCode = appError.Code err = appError.RawError msg = appError.Msg diff --git a/routers/controllers/callback.go b/routers/controllers/callback.go index dade566..ba3ab46 100644 --- a/routers/controllers/callback.go +++ b/routers/controllers/callback.go @@ -57,7 +57,7 @@ func UpyunCallback(c *gin.Context) { if err := c.ShouldBind(&callbackBody); err == nil { if callbackBody.Code != 200 { util.Log().Debug( - "又拍云回调返回错误代码%d,信息:%s", + "Upload callback returned error code:%d, message: %s", callbackBody.Code, callbackBody.Message, ) diff --git a/service/admin/file.go b/service/admin/file.go index 8c6c6e9..1d7bc2b 100644 --- a/service/admin/file.go +++ b/service/admin/file.go @@ -42,7 +42,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response { // 创建文件系统 fs, err := filesystem.NewAnonymousFileSystem() if err != nil { - return serializer.Err(serializer.CodeInternalSetting, "Failed to create filesystem.", err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() @@ -69,7 +69,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response { // 创建文件系统 fs, err := filesystem.NewFileSystem(&user) if err != nil { - return serializer.Err(serializer.CodeInternalSetting, "Failed to create filesystem.", err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() diff --git a/service/aria2/add.go b/service/aria2/add.go index 6b86069..52b5964 100644 --- a/service/aria2/add.go +++ b/service/aria2/add.go @@ -24,24 +24,24 @@ func (service *BatchAddURLService) Add(c *gin.Context, taskType int) serializer. // 创建文件系统 fs, err := filesystem.NewFileSystemFromContext(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() // 检查用户组权限 if !fs.User.Group.OptionsSerialized.Aria2 { - return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil) + return serializer.Err(serializer.CodeGroupNotAllowed, "", nil) } // 存放目录是否存在 if exist, _ := fs.IsPathExist(service.Dst); !exist { - return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil) + return serializer.Err(serializer.CodeParentNotExist, "", nil) } // 检查批量任务数量 limit := fs.User.Group.OptionsSerialized.Aria2BatchSize if limit > 0 && len(service.URLs) > limit { - return serializer.Err(serializer.CodeBatchAria2Size, "Exceed aria2 batch size", nil) + return serializer.Err(serializer.CodeBatchAria2Size, "", nil) } res := make([]serializer.Response, 0, len(service.URLs)) @@ -71,25 +71,25 @@ func (service *AddURLService) Add(c *gin.Context, fs *filesystem.FileSystem, tas // 创建文件系统 fs, err = filesystem.NewFileSystemFromContext(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() // 检查用户组权限 if !fs.User.Group.OptionsSerialized.Aria2 { - return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil) + return serializer.Err(serializer.CodeGroupNotAllowed, "", nil) } // 存放目录是否存在 if exist, _ := fs.IsPathExist(service.Dst); !exist { - return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil) + return serializer.Err(serializer.CodeParentNotExist, "", nil) } } downloads := model.GetDownloadsByStatusAndUser(0, fs.User.ID, common.Downloading, common.Paused, common.Ready) limit := fs.User.Group.OptionsSerialized.Aria2BatchSize if limit > 0 && len(downloads)+1 > limit { - return serializer.Err(serializer.CodeBatchAria2Size, "Exceed aria2 batch size", nil) + return serializer.Err(serializer.CodeBatchAria2Size, "", nil) } // 创建任务 @@ -107,20 +107,20 @@ func (service *AddURLService) Add(c *gin.Context, fs *filesystem.FileSystem, tas // 获取 Aria2 实例 err, node := cluster.Default.BalanceNodeByFeature("aria2", lb) if err != nil { - return serializer.Err(serializer.CodeInternalSetting, "Aria2 实例获取失败", err) + return serializer.Err(serializer.CodeInternalSetting, "Failed to get Aria2 instance", err) } // 创建任务 gid, err := node.GetAria2Instance().CreateTask(task, fs.User.Group.OptionsSerialized.Aria2Options) if err != nil { - return serializer.Err(serializer.CodeNotSet, "任务创建失败", err) + return serializer.Err(serializer.CodeCreateTaskError, "", err) } task.GID = gid task.NodeID = node.ID() _, err = task.Create() if err != nil { - return serializer.DBErr("任务创建失败", err) + return serializer.DBErr("Failed to create task record", err) } // 创建任务监控 diff --git a/service/aria2/manage.go b/service/aria2/manage.go index 34e17b9..a8cd7f6 100644 --- a/service/aria2/manage.go +++ b/service/aria2/manage.go @@ -54,13 +54,13 @@ func (service *DownloadTaskService) Delete(c *gin.Context) serializer.Response { // 查找下载记录 download, err := model.GetDownloadByGid(c.Param("gid"), user.ID) if err != nil { - return serializer.Err(serializer.CodeNotFound, "下载记录不存在", err) + return serializer.Err(serializer.CodeNotFound, "Download record not found", err) } if download.Status >= common.Error { // 如果任务已完成,则删除任务记录 if err := download.Delete(); err != nil { - return serializer.Err(serializer.CodeDBError, "任务记录删除失败", err) + return serializer.DBErr("Failed to delete task record", err) } return serializer.Response{} } @@ -68,11 +68,11 @@ func (service *DownloadTaskService) Delete(c *gin.Context) serializer.Response { // 取消任务 node := cluster.Default.GetNodeByID(download.GetNodeID()) if node == nil { - return serializer.Err(serializer.CodeInternalSetting, "目标节点不可用", err) + return serializer.Err(serializer.CodeNodeOffline, "", err) } if err := node.GetAria2Instance().Cancel(download); err != nil { - return serializer.Err(serializer.CodeNotSet, "操作失败", err) + return serializer.Err(serializer.CodeNotSet, "Operation failed", err) } return serializer.Response{} @@ -86,17 +86,17 @@ func (service *SelectFileService) Select(c *gin.Context) serializer.Response { // 查找下载记录 download, err := model.GetDownloadByGid(c.Param("gid"), user.ID) if err != nil { - return serializer.Err(serializer.CodeNotFound, "下载记录不存在", err) + return serializer.Err(serializer.CodeNotFound, "Download record not found", err) } if download.StatusInfo.BitTorrent.Mode != "multi" || (download.Status != common.Downloading && download.Status != common.Paused) { - return serializer.Err(serializer.CodeNoPermissionErr, "此下载任务无法选取文件", err) + return serializer.ParamErr("You cannot select files for this task", nil) } // 选取下载 node := cluster.Default.GetNodeByID(download.GetNodeID()) if err := node.GetAria2Instance().Select(download, service.Indexes); err != nil { - return serializer.Err(serializer.CodeNotSet, "操作失败", err) + return serializer.Err(serializer.CodeNotSet, "Operation failed", err) } return serializer.Response{} diff --git a/service/callback/upload.go b/service/callback/upload.go index 698d50c..25390bc 100644 --- a/service/callback/upload.go +++ b/service/callback/upload.go @@ -112,7 +112,7 @@ func ProcessCallback(service CallbackProcessService, c *gin.Context) serializer. // 创建文件系统 fs, err := filesystem.NewFileSystemFromCallback(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, err.Error(), err) } defer fs.Recycle() @@ -156,7 +156,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response // 创建文件系统 fs, err := filesystem.NewFileSystemFromCallback(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() @@ -166,7 +166,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response // 获取文件信息 info, err := fs.Handler.(onedrive.Driver).Client.Meta(context.Background(), "", uploadSession.SavePath) if err != nil { - return serializer.Err(serializer.CodeUploadFailed, "文件元信息查询失败", err) + return serializer.Err(serializer.CodeQueryMetaFailed, "", err) } // 验证与回调会话中是否一致 @@ -181,7 +181,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response if isSizeCheckFailed || !strings.EqualFold(info.GetSourcePath(), actualPath) { fs.Handler.(onedrive.Driver).Client.Delete(context.Background(), []string{info.GetSourcePath()}) - return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) + return serializer.Err(serializer.CodeMetaMismatch, "", err) } service.Meta = info return ProcessCallback(service, c) @@ -192,7 +192,7 @@ func (service *COSCallback) PreProcess(c *gin.Context) serializer.Response { // 创建文件系统 fs, err := filesystem.NewFileSystemFromCallback(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() @@ -202,12 +202,12 @@ func (service *COSCallback) PreProcess(c *gin.Context) serializer.Response { // 获取文件信息 info, err := fs.Handler.(cos.Driver).Meta(context.Background(), uploadSession.SavePath) if err != nil { - return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) + return serializer.Err(serializer.CodeMetaMismatch, "", err) } // 验证实际文件信息与回调会话中是否一致 if uploadSession.Size != info.Size || uploadSession.Key != info.CallbackKey { - return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) + return serializer.Err(serializer.CodeMetaMismatch, "", err) } return ProcessCallback(service, c) @@ -218,7 +218,7 @@ func (service *S3Callback) PreProcess(c *gin.Context) serializer.Response { // 创建文件系统 fs, err := filesystem.NewFileSystemFromCallback(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() @@ -228,12 +228,12 @@ func (service *S3Callback) PreProcess(c *gin.Context) serializer.Response { // 获取文件信息 info, err := fs.Handler.(*s3.Driver).Meta(context.Background(), uploadSession.SavePath) if err != nil { - return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) + return serializer.Err(serializer.CodeMetaMismatch, "", err) } // 验证实际文件信息与回调会话中是否一致 if uploadSession.Size != info.Size { - return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) + return serializer.Err(serializer.CodeMetaMismatch, "", err) } return ProcessCallback(service, c) @@ -244,7 +244,7 @@ func (service *UploadCallbackService) PreProcess(c *gin.Context) serializer.Resp // 创建文件系统 fs, err := filesystem.NewFileSystemFromCallback(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() @@ -254,7 +254,7 @@ func (service *UploadCallbackService) PreProcess(c *gin.Context) serializer.Resp // 验证文件大小 if uploadSession.Size != service.Size { fs.Handler.Delete(context.Background(), []string{uploadSession.SavePath}) - return serializer.Err(serializer.CodeUploadFailed, "文件大小不一致", nil) + return serializer.Err(serializer.CodeMetaMismatch, "", err) } return ProcessCallback(service, c) diff --git a/service/explorer/directory.go b/service/explorer/directory.go index 844f812..cd03999 100644 --- a/service/explorer/directory.go +++ b/service/explorer/directory.go @@ -18,7 +18,7 @@ func (service *DirectoryService) ListDirectory(c *gin.Context) serializer.Respon // 创建文件系统 fs, err := filesystem.NewFileSystemFromContext(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() @@ -48,7 +48,7 @@ func (service *DirectoryService) CreateDirectory(c *gin.Context) serializer.Resp // 创建文件系统 fs, err := filesystem.NewFileSystemFromContext(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle() diff --git a/service/explorer/file.go b/service/explorer/file.go index 15c88b2..cb4ecfe 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -267,7 +267,7 @@ func (service *DownloadService) Download(ctx context.Context, c *gin.Context) se // 创建文件系统 fs, err := filesystem.NewFileSystemFromContext(c) if err != nil { - return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + return serializer.Err(serializer.CodeCreateFSError, "", err) } defer fs.Recycle()