i18n: error codes for dashboard operations

pull/1416/head
HFO4 2 years ago
parent 9869671633
commit a1880672b1

@ -1 +1 @@
Subproject commit 41f585a6f8c8f99ed4b2e279555d6b4dcdf957bc
Subproject commit f47af47efeecae015dd28a76ab797665391029e0

@ -118,6 +118,30 @@ const (
CodeEmailSent = 40033
// CodeUserCannotActivate 用户无法激活
CodeUserCannotActivate = 40034
// 存储策略不存在
CodePolicyNotExist = 40035
// 无法删除默认存储策略
CodeDeleteDefaultPolicy = 40036
// 存储策略下还有文件
CodePolicyUsedByFiles = 40037
// 存储策略绑定了用户组
CodePolicyUsedByGroups = 40038
// 用户组不存在
CodeGroupNotFound = 40039
// 对系统用户组执行非法操作
CodeInvalidActionOnSystemGroup = 40040
// 用户组正在被使用
CodeGroupUsedByUser = 40041
// 为初始用户更改用户组
CodeChangeGroupForDefaultUser = 40042
// 对系统用户执行非法操作
CodeInvalidActionOnDefaultUser = 40043
// 文件不存在
CodeFileNotFound = 40044
// 列取文件失败
CodeListFilesError = 40045
// 对系统节点进行非法操作
CodeInvalidActionOnSystemNode = 40046
// CodeDBError 数据库操作失败
CodeDBError = 50001
// CodeEncryptError 加密失败
@ -130,6 +154,10 @@ const (
CodeCacheOperation = 50006
// CodeCallbackError 回调失败
CodeCallbackError = 50007
// 后台设置更新失败
CodeUpdateSetting = 50008
// 跨域策略添加失败
CodeAddCORS = 50009
//CodeParamErr 各种奇奇怪怪的参数错误
CodeParamErr = 40001
// CodeNotSet 未定错误后续尝试从error中获取

@ -26,11 +26,11 @@ type Aria2TestService struct {
func (service *Aria2TestService) TestMaster() serializer.Response {
res, err := aria2.TestRPCConnection(service.RPC, service.Token, 5)
if err != nil {
return serializer.ParamErr(err.Error(), err)
return serializer.ParamErr("Failed to connect to RPC server: "+err.Error(), err)
}
if res.Version == "" {
return serializer.ParamErr("RPC 服务返回非预期响应", nil)
return serializer.ParamErr("RPC server returns unexpected response", nil)
}
return serializer.Response{Data: res.Version}
@ -39,7 +39,7 @@ func (service *Aria2TestService) TestMaster() serializer.Response {
func (service *Aria2TestService) TestSlave() serializer.Response {
slave, err := url.Parse(service.Server)
if err != nil {
return serializer.ParamErr("无法解析从机端地址,"+err.Error(), nil)
return serializer.ParamErr("Cannot parse slave server URL, "+err.Error(), nil)
}
controller, _ := url.Parse("/api/v3/slave/ping/aria2")
@ -60,11 +60,11 @@ func (service *Aria2TestService) TestSlave() serializer.Response {
),
).DecodeResponse()
if err != nil {
return serializer.ParamErr("无连接到从机,"+err.Error(), nil)
return serializer.ParamErr("Failed to connect to slave node, "+err.Error(), nil)
}
if res.Code != 0 {
return serializer.ParamErr("成功接到从机,但是从机返回:"+res.Msg, nil)
return serializer.ParamErr("Successfully connected to slave, but slave returns: "+res.Msg, nil)
}
return serializer.Response{Data: res.Data.(string)}

@ -36,13 +36,13 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
// 列取存储策略中的目录
policy, err := model.GetPolicyByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", err)
return serializer.Err(serializer.CodePolicyNotExist, "", err)
}
// 创建文件系统
fs, err := filesystem.NewAnonymousFileSystem()
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "无法创建文件系统", err)
return serializer.Err(serializer.CodeInternalSetting, "Failed to create filesystem.", err)
}
defer fs.Recycle()
@ -50,7 +50,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
fs.Policy = &policy
res, err := fs.ListPhysical(c.Request.Context(), service.Path)
if err != nil {
return serializer.Err(serializer.CodeIOFailed, "无法列取目录", err)
return serializer.Err(serializer.CodeListFilesError, "", err)
}
return serializer.Response{
@ -63,20 +63,20 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
// 查找用户
user, err := model.GetUserByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "用户不存在", err)
return serializer.Err(serializer.CodeUserNotFound, "用户不存在", err)
}
// 创建文件系统
fs, err := filesystem.NewFileSystem(&user)
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "无法创建文件系统", err)
return serializer.Err(serializer.CodeInternalSetting, "Failed to create filesystem.", err)
}
defer fs.Recycle()
// 列取目录
res, err := fs.List(c.Request.Context(), service.Path, nil)
if err != nil {
return serializer.Err(serializer.CodeIOFailed, "无法列取目录", err)
return serializer.Err(serializer.CodeListFilesError, "", err)
}
return serializer.Response{
@ -88,7 +88,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
func (service *FileBatchService) Delete(c *gin.Context) serializer.Response {
files, err := model.GetFilesByIDs(service.ID, 0)
if err != nil {
return serializer.DBErr("无法列出待删除文件", err)
return serializer.DBErr("Failed to list files for deleting", err)
}
// 根据用户分组
@ -135,7 +135,7 @@ func (service *FileBatchService) Delete(c *gin.Context) serializer.Response {
func (service *FileService) Get(c *gin.Context) serializer.Response {
file, err := model.GetFilesByIDs([]uint{service.ID}, 0)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "文件不存在", err)
return serializer.Err(serializer.CodeFileNotFound, "", err)
}
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, &file[0])

@ -1,10 +1,9 @@
package admin
import (
"fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"strconv"
)
// AddGroupService 用户组添加服务
@ -21,7 +20,7 @@ type GroupService struct {
func (service *GroupService) Get() serializer.Response {
group, err := model.GetGroupByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", err)
return serializer.Err(serializer.CodeGroupNotFound, "", err)
}
return serializer.Response{Data: group}
@ -32,12 +31,12 @@ func (service *GroupService) Delete() serializer.Response {
// 查找用户组
group, err := model.GetGroupByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "用户组不存在", err)
return serializer.Err(serializer.CodeGroupNotFound, "", err)
}
// 是否为系统用户组
if group.ID <= 3 {
return serializer.Err(serializer.CodeNoPermissionErr, "系统用户组无法删除", err)
return serializer.Err(serializer.CodeInvalidActionOnSystemGroup, "", err)
}
// 检查是否有用户使用
@ -46,7 +45,7 @@ func (service *GroupService) Delete() serializer.Response {
Select("count(id)").Row()
row.Scan(&total)
if total > 0 {
return serializer.ParamErr(fmt.Sprintf("有 %d 位用户仍属于此用户组,请先删除这些用户或者更改用户组", total), nil)
return serializer.Err(serializer.CodeGroupUsedByUser, strconv.Itoa(total), nil)
}
model.DB.Delete(&group)
@ -58,11 +57,11 @@ func (service *GroupService) Delete() serializer.Response {
func (service *AddGroupService) Add() serializer.Response {
if service.Group.ID > 0 {
if err := model.DB.Save(&service.Group).Error; err != nil {
return serializer.ParamErr("用户组保存失败", err)
return serializer.DBErr("Failed to save group record", err)
}
} else {
if err := model.DB.Create(&service.Group).Error; err != nil {
return serializer.ParamErr("用户组添加失败", err)
return serializer.DBErr("Failed to create group record", err)
}
}

@ -16,11 +16,11 @@ type AddNodeService struct {
func (service *AddNodeService) Add() serializer.Response {
if service.Node.ID > 0 {
if err := model.DB.Save(&service.Node).Error; err != nil {
return serializer.ParamErr("节点保存失败", err)
return serializer.DBErr("Failed to save node record", err)
}
} else {
if err := model.DB.Create(&service.Node).Error; err != nil {
return serializer.ParamErr("节点添加失败", err)
return serializer.DBErr("Failed to create node record", err)
}
}
@ -84,16 +84,16 @@ type ToggleNodeService struct {
func (service *ToggleNodeService) Toggle() serializer.Response {
node, err := model.GetNodeByID(service.ID)
if err != nil {
return serializer.DBErr("找不到节点", err)
return serializer.DBErr("Node not found", err)
}
// 是否为系统节点
if node.ID <= 1 {
return serializer.Err(serializer.CodeNoPermissionErr, "系统节点无法更改", err)
return serializer.Err(serializer.CodeInvalidActionOnSystemNode, "", err)
}
if err = node.SetStatus(service.Desired); err != nil {
return serializer.DBErr("无法更改节点状态", err)
return serializer.DBErr("Failed to change node status", err)
}
if service.Desired == model.NodeActive {
@ -115,17 +115,17 @@ func (service *NodeService) Delete() serializer.Response {
// 查找用户组
node, err := model.GetNodeByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "节点不存在", err)
return serializer.DBErr("Node record not found", err)
}
// 是否为系统节点
if node.ID <= 1 {
return serializer.Err(serializer.CodeNoPermissionErr, "系统节点无法删除", err)
return serializer.Err(serializer.CodeInvalidActionOnSystemNode, "", err)
}
cluster.Default.Delete(node.ID)
if err := model.DB.Delete(&node).Error; err != nil {
return serializer.DBErr("无法删除节点", err)
return serializer.DBErr("Failed to delete node record", err)
}
return serializer.Response{}
@ -135,7 +135,7 @@ func (service *NodeService) Delete() serializer.Response {
func (service *NodeService) Get() serializer.Response {
node, err := model.GetNodeByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "节点不存在", err)
return serializer.DBErr("Node not exist", err)
}
return serializer.Response{Data: node}

@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -58,12 +59,12 @@ type PolicyService struct {
func (service *PolicyService) Delete() serializer.Response {
// 禁止删除默认策略
if service.ID == 1 {
return serializer.Err(serializer.CodeNoPermissionErr, "默认存储策略无法删除", nil)
return serializer.Err(serializer.CodeDeleteDefaultPolicy, "", nil)
}
policy, err := model.GetPolicyByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", err)
return serializer.Err(serializer.CodePolicyNotExist, "", err)
}
// 检查是否有文件使用
@ -72,7 +73,7 @@ func (service *PolicyService) Delete() serializer.Response {
Select("count(id)").Row()
row.Scan(&total)
if total > 0 {
return serializer.ParamErr(fmt.Sprintf("有 %d 个文件仍在使用此存储策略,请先删除这些文件", total), nil)
return serializer.Err(serializer.CodePolicyUsedByFiles, strconv.Itoa(total), nil)
}
// 检查用户组使用
@ -83,7 +84,7 @@ func (service *PolicyService) Delete() serializer.Response {
).Find(&groups)
if len(groups) > 0 {
return serializer.ParamErr(fmt.Sprintf("有 %d 个用户组绑定了此存储策略,请先解除绑定", len(groups)), nil)
return serializer.Err(serializer.CodePolicyUsedByGroups, strconv.Itoa(len(groups)), nil)
}
model.DB.Delete(&policy)
@ -96,7 +97,7 @@ func (service *PolicyService) Delete() serializer.Response {
func (service *PolicyService) Get() serializer.Response {
policy, err := model.GetPolicyByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", err)
return serializer.Err(serializer.CodePolicyNotExist, "", err)
}
return serializer.Response{Data: policy}
@ -106,12 +107,12 @@ func (service *PolicyService) Get() serializer.Response {
func (service *PolicyService) GetOAuth(c *gin.Context) serializer.Response {
policy, err := model.GetPolicyByID(service.ID)
if err != nil || policy.Type != "onedrive" {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", nil)
return serializer.Err(serializer.CodePolicyNotExist, "", nil)
}
client, err := onedrive.NewClient(&policy)
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "无法初始化 OneDrive 客户端", err)
return serializer.Err(serializer.CodeInternalSetting, "Failed to initialize OneDrive client", err)
}
util.SetSession(c, map[string]interface{}{
@ -130,11 +131,11 @@ func (service *PolicyService) GetOAuth(c *gin.Context) serializer.Response {
func (service *PolicyService) AddSCF() serializer.Response {
policy, err := model.GetPolicyByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", nil)
return serializer.Err(serializer.CodePolicyNotExist, "", nil)
}
if err := cos.CreateSCF(&policy, service.Region); err != nil {
return serializer.Err(serializer.CodeInternalSetting, "云函数创建失败", err)
return serializer.ParamErr("Failed to create SCF function", err)
}
return serializer.Response{}
@ -144,17 +145,17 @@ func (service *PolicyService) AddSCF() serializer.Response {
func (service *PolicyService) AddCORS() serializer.Response {
policy, err := model.GetPolicyByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", nil)
return serializer.Err(serializer.CodePolicyNotExist, "", nil)
}
switch policy.Type {
case "oss":
handler, err := oss.NewDriver(&policy)
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "跨域策略添加失败", err)
return serializer.Err(serializer.CodeAddCORS, "", err)
}
if err := handler.CORS(); err != nil {
return serializer.Err(serializer.CodeInternalSetting, "跨域策略添加失败", err)
return serializer.Err(serializer.CodeAddCORS, "", err)
}
case "cos":
u, _ := url.Parse(policy.Server)
@ -171,19 +172,19 @@ func (service *PolicyService) AddCORS() serializer.Response {
}
if err := handler.CORS(); err != nil {
return serializer.Err(serializer.CodeInternalSetting, "跨域策略添加失败", err)
return serializer.Err(serializer.CodeAddCORS, "", err)
}
case "s3":
handler, err := s3.NewDriver(&policy)
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "跨域策略添加失败", err)
return serializer.Err(serializer.CodeAddCORS, "", err)
}
if err := handler.CORS(); err != nil {
return serializer.Err(serializer.CodeInternalSetting, "跨域策略添加失败", err)
return serializer.Err(serializer.CodeAddCORS, "", err)
}
default:
return serializer.ParamErr("不支持此策略", nil)
return serializer.Err(serializer.CodePolicyNotAllowed, "", nil)
}
return serializer.Response{}
@ -225,7 +226,7 @@ func (service *SlavePingService) Test() serializer.Response {
func (service *SlaveTestService) Test() serializer.Response {
slave, err := url.Parse(service.Server)
if err != nil {
return serializer.ParamErr("无法解析从机端地址,"+err.Error(), nil)
return serializer.ParamErr("Failed to parse slave node server URL: "+err.Error(), nil)
}
controller, _ := url.Parse("/api/v3/slave/ping")
@ -248,11 +249,11 @@ func (service *SlaveTestService) Test() serializer.Response {
),
).DecodeResponse()
if err != nil {
return serializer.ParamErr("无连接到从机,"+err.Error(), nil)
return serializer.ParamErr("Failed to connect to slave node: "+err.Error(), nil)
}
if res.Code != 0 {
return serializer.ParamErr("成功接到从机,但是从机返回:"+res.Msg, nil)
return serializer.ParamErr("Successfully connected to slave node, but slave returns: "+res.Msg, nil)
}
return serializer.Response{}
@ -266,11 +267,11 @@ func (service *AddPolicyService) Add() serializer.Response {
if service.Policy.ID > 0 {
if err := model.DB.Save(&service.Policy).Error; err != nil {
return serializer.ParamErr("存储策略保存失败", err)
return serializer.DBErr("Failed to save policy", err)
}
} else {
if err := model.DB.Create(&service.Policy).Error; err != nil {
return serializer.ParamErr("存储策略添加失败", err)
return serializer.DBErr("Failed to create policy", err)
}
}
@ -286,7 +287,7 @@ func (service *PathTestService) Test() serializer.Response {
path = filepath.Join(path, "test.txt")
file, err := util.CreatNestedFile(util.RelativePath(path))
if err != nil {
return serializer.ParamErr(fmt.Sprintf("无法创建路径 %s , %s", path, err.Error()), nil)
return serializer.ParamErr(fmt.Sprintf("Failed to create \"%s\": %s", path, err.Error()), nil)
}
file.Close()

@ -17,7 +17,7 @@ type ShareBatchService struct {
// Delete 删除文件
func (service *ShareBatchService) Delete(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Share{}).Error; err != nil {
return serializer.DBErr("无法删除分享", err)
return serializer.DBErr("Failed to delete share record", err)
}
return serializer.Response{}
}

@ -43,8 +43,8 @@ type MailTestService struct {
// Send 发送测试邮件
func (service *MailTestService) Send() serializer.Response {
if err := email.Send(service.Email, "Cloudreve发信测试", "这是一封测试邮件,用于测试 Cloudreve 发信设置。"); err != nil {
return serializer.Err(serializer.CodeInternalSetting, "发信失败, "+err.Error(), nil)
if err := email.Send(service.Email, "Cloudreve Email delivery test", "This is a test Email, to test Cloudreve Email delivery settings"); err != nil {
return serializer.Err(serializer.CodeFailedSendEmail, err.Error(), nil)
}
return serializer.Response{}
}
@ -65,14 +65,14 @@ func (service *BatchSettingChangeService) Change() serializer.Response {
if err := tx.Model(&model.Setting{}).Where("name = ?", setting.Key).Update("value", setting.Value).Error; err != nil {
cache.Deletes(cacheClean, "setting_")
tx.Rollback()
return serializer.DBErr("设置 "+setting.Key+" 更新失败", err)
return serializer.Err(serializer.CodeUpdateSetting, "Setting "+setting.Key+" failed to update", err)
}
cacheClean = append(cacheClean, setting.Key)
}
if err := tx.Commit().Error; err != nil {
return serializer.DBErr("设置更新失败", err)
return serializer.DBErr("Failed to update setting", err)
}
cache.Deletes(cacheClean, "setting_")

@ -28,7 +28,7 @@ func (service *ImportTaskService) Create(c *gin.Context, user *model.User) seria
// 创建任务
job, err := task.NewImportTask(service.UID, service.PolicyID, service.Src, service.Dst, service.Recursive)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "任务创建失败", err)
return serializer.DBErr("Failed to create task record.", err)
}
task.TaskPoll.Submit(job)
return serializer.Response{}
@ -37,7 +37,7 @@ func (service *ImportTaskService) Create(c *gin.Context, user *model.User) seria
// Delete 删除任务
func (service *TaskBatchService) Delete(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Download{}).Error; err != nil {
return serializer.DBErr("无法删除任务", err)
return serializer.DBErr("Failed to delete task records", err)
}
return serializer.Response{}
}
@ -45,7 +45,7 @@ func (service *TaskBatchService) Delete(c *gin.Context) serializer.Response {
// DeleteGeneral 删除常规任务
func (service *TaskBatchService) DeleteGeneral(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Task{}).Error; err != nil {
return serializer.DBErr("无法删除任务", err)
return serializer.DBErr("Failed to delete task records", err)
}
return serializer.Response{}
}

@ -29,11 +29,11 @@ type UserBatchService struct {
func (service *UserService) Ban() serializer.Response {
user, err := model.GetUserByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "用户不存在", err)
return serializer.Err(serializer.CodeUserNotFound, "", err)
}
if user.ID == 1 {
return serializer.Err(serializer.CodeNoPermissionErr, "无法封禁初始用户", err)
return serializer.Err(serializer.CodeInvalidActionOnDefaultUser, "", err)
}
if user.Status == model.Active {
@ -50,12 +50,12 @@ func (service *UserBatchService) Delete() serializer.Response {
for _, uid := range service.ID {
user, err := model.GetUserByID(uid)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "用户不存在", err)
return serializer.Err(serializer.CodeUserNotFound, "", err)
}
// 不能删除初始用户
if uid == 1 {
return serializer.Err(serializer.CodeNoPermissionErr, "无法删除初始用户", err)
return serializer.Err(serializer.CodeInvalidActionOnDefaultUser, "", err)
}
// 删除与此用户相关的所有资源
@ -64,7 +64,7 @@ func (service *UserBatchService) Delete() serializer.Response {
// 删除所有文件
root, err := fs.User.Root()
if err != nil {
return serializer.Err(serializer.CodeNotFound, "无法找到用户根目录", err)
return serializer.Err(serializer.CodeInternalSetting, "User's root folder not exist", err)
}
fs.Delete(context.Background(), []uint{root.ID}, []uint{}, false)
@ -89,7 +89,7 @@ func (service *UserBatchService) Delete() serializer.Response {
func (service *UserService) Get() serializer.Response {
group, err := model.GetUserByID(service.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "用户不存在", err)
return serializer.Err(serializer.CodeUserNotFound, "", err)
}
return serializer.Response{Data: group}
@ -112,16 +112,16 @@ func (service *AddUserService) Add() serializer.Response {
// 检查愚蠢操作
if user.ID == 1 && user.GroupID != 1 {
return serializer.ParamErr("无法更改初始用户的用户组", nil)
return serializer.Err(serializer.CodeChangeGroupForDefaultUser, "", nil)
}
if err := model.DB.Save(&user).Error; err != nil {
return serializer.ParamErr("用户保存失败", err)
return serializer.DBErr("Failed to save user record", err)
}
} else {
service.User.SetPassword(service.Password)
if err := model.DB.Create(&service.User).Error; err != nil {
return serializer.ParamErr("用户组添加失败", err)
return serializer.DBErr("Failed to create user record", err)
}
}

Loading…
Cancel
Save