Skip to content

Commit

Permalink
chore: 统一错误输出
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 1, 2023
1 parent 1fa96e8 commit 00ec8d6
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion app/http/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Check(ctx http.Context, slug string) http.Response {
installedPlugins, err := services.NewPluginImpl().AllInstalled()
if err != nil {
facades.Log().Error("[面板][插件] 获取已安装插件失败")
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if installedPlugin.Version != plugin.Version || installedPlugin.Slug != plugin.Slug {
Expand Down
14 changes: 7 additions & 7 deletions app/http/controllers/cron_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *CronController) List(ctx http.Context) http.Response {
err := facades.Orm().Query().Paginate(page, limit, &crons, &total)
if err != nil {
facades.Log().Error("[面板][CronController] 查询计划任务列表失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, http.Json{
Expand Down Expand Up @@ -121,7 +121,7 @@ panel cutoff ${name} ${save} 2>&1
shellFile := strconv.Itoa(int(carbon.Now().Timestamp())) + tools.RandomString(16)
if !tools.Write(shellDir+shellFile+".sh", shell, 0700) {
facades.Log().Error("[面板][CronController] 创建计划任务脚本失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
tools.Exec("dos2unix " + shellDir + shellFile + ".sh")

Expand All @@ -136,7 +136,7 @@ panel cutoff ${name} ${save} 2>&1
err = facades.Orm().Query().Create(&cron)
if err != nil {
facades.Log().Error("[面板][CronController] 创建计划任务失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

c.cron.AddToSystem(cron)
Expand Down Expand Up @@ -191,12 +191,12 @@ func (c *CronController) Update(ctx http.Context) http.Response {
err = facades.Orm().Query().Save(&cron)
if err != nil {
facades.Log().Error("[面板][CronController] 更新计划任务失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if !tools.Write(cron.Shell, ctx.Request().Input("script"), 0644) {
facades.Log().Error("[面板][CronController] 更新计划任务脚本失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
tools.Exec("dos2unix " + cron.Shell)

Expand All @@ -222,7 +222,7 @@ func (c *CronController) Delete(ctx http.Context) http.Response {
_, err = facades.Orm().Query().Delete(&cron)
if err != nil {
facades.Log().Error("[面板][CronController] 删除计划任务失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, nil)
Expand Down Expand Up @@ -250,7 +250,7 @@ func (c *CronController) Status(ctx http.Context) http.Response {
err = facades.Orm().Query().Save(&cron)
if err != nil {
facades.Log().Error("[面板][CronController] 更新计划任务状态失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

c.cron.DeleteFromSystem(cron)
Expand Down
6 changes: 3 additions & 3 deletions app/http/controllers/info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *InfoController) Name(ctx http.Context) http.Response {
err := facades.Orm().Query().Where("key", "name").First(&setting)
if err != nil {
facades.Log().Error("[面板][InfoController] 查询面板名称失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, http.Json{
Expand All @@ -53,7 +53,7 @@ func (c *InfoController) HomePlugins(ctx http.Context) http.Response {
err := facades.Orm().Query().Where("show", 1).Find(&plugins)
if err != nil {
facades.Log().Error("[面板][InfoController] 查询首页插件失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

type pluginsData struct {
Expand Down Expand Up @@ -199,7 +199,7 @@ func (c *InfoController) InstalledDbAndPhp(ctx http.Context) http.Response {
var php []models.Plugin
err := facades.Orm().Query().Where("slug like ?", "php%").Find(&php)
if err != nil {
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

var mysql models.Plugin
Expand Down
8 changes: 4 additions & 4 deletions app/http/controllers/monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *MonitorController) Switch(ctx http.Context) http.Response {
err := r.setting.Set(models.SettingKeyMonitor, cast.ToString(value))
if err != nil {
facades.Log().Error("[面板][MonitorController] 更新监控开关失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, nil)
Expand All @@ -39,7 +39,7 @@ func (r *MonitorController) SaveDays(ctx http.Context) http.Response {
err := r.setting.Set(models.SettingKeyMonitorDays, days)
if err != nil {
facades.Log().Error("[面板][MonitorController] 更新监控天数失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, nil)
Expand All @@ -61,7 +61,7 @@ func (r *MonitorController) Clear(ctx http.Context) http.Response {
_, err := facades.Orm().Query().Where("1 = 1").Delete(&models.Monitor{})
if err != nil {
facades.Log().Error("[面板][MonitorController] 清空监控数据失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, nil)
Expand All @@ -78,7 +78,7 @@ func (r *MonitorController) List(ctx http.Context) http.Response {
err := facades.Orm().Query().Where("created_at >= ?", startTime.ToDateTimeString()).Where("created_at <= ?", endTime.ToDateTimeString()).Get(&monitors)
if err != nil {
facades.Log().Error("[面板][MonitorController] 查询监控数据失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if len(monitors) == 0 {
Expand Down
18 changes: 9 additions & 9 deletions app/http/controllers/plugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *PluginController) List(ctx http.Context) http.Response {
plugins := r.plugin.All()
installedPlugins, err := r.plugin.AllInstalled()
if err != nil {
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

var lock sync.RWMutex
Expand Down Expand Up @@ -101,7 +101,7 @@ func (r *PluginController) Install(ctx http.Context) http.Response {
installedPlugins, err := r.plugin.AllInstalled()
if err != nil {
facades.Log().Error("[面板][PluginController] 获取已安装插件失败")
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if installedPlugin.ID != 0 {
Expand Down Expand Up @@ -142,7 +142,7 @@ func (r *PluginController) Install(ctx http.Context) http.Response {
task.Log = "/tmp/" + plugin.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[面板][PluginController] 创建任务失败: " + err.Error())
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand All @@ -157,7 +157,7 @@ func (r *PluginController) Uninstall(ctx http.Context) http.Response {
installedPlugins, err := r.plugin.AllInstalled()
if err != nil {
facades.Log().Error("[面板][PluginController] 获取已安装插件失败")
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if installedPlugin.ID == 0 {
Expand Down Expand Up @@ -198,7 +198,7 @@ func (r *PluginController) Uninstall(ctx http.Context) http.Response {
task.Log = "/tmp/" + plugin.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[面板][PluginController] 创建任务失败: " + err.Error())
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand All @@ -213,7 +213,7 @@ func (r *PluginController) Update(ctx http.Context) http.Response {
installedPlugins, err := r.plugin.AllInstalled()
if err != nil {
facades.Log().Error("[面板][PluginController] 获取已安装插件失败")
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if installedPlugin.ID == 0 {
Expand Down Expand Up @@ -254,7 +254,7 @@ func (r *PluginController) Update(ctx http.Context) http.Response {
task.Log = "/tmp/" + plugin.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[面板][PluginController] 创建任务失败: " + err.Error())
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand All @@ -269,7 +269,7 @@ func (r *PluginController) UpdateShow(ctx http.Context) http.Response {
var plugin models.Plugin
if err := facades.Orm().Query().Where("slug", slug).First(&plugin); err != nil {
facades.Log().Error("[面板][PluginController] 查询插件失败: " + err.Error())
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
if plugin.ID == 0 {
return Error(ctx, http.StatusUnprocessableEntity, "插件未安装")
Expand All @@ -278,7 +278,7 @@ func (r *PluginController) UpdateShow(ctx http.Context) http.Response {
plugin.Show = show
if err := facades.Orm().Query().Save(&plugin); err != nil {
facades.Log().Error("[面板][PluginController] 更新插件失败: " + err.Error())
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, "操作成功")
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/php74_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (r *Php74Controller) InstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建安装拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *Php74Controller) UninstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建卸载拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/php80_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (r *Php80Controller) InstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建安装拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *Php80Controller) UninstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建卸载拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/php81_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (r *Php81Controller) InstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建安装拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *Php81Controller) UninstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建卸载拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/php82_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (r *Php82Controller) InstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建安装拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *Php82Controller) UninstallExtension(ctx http.Context) http.Response {
task.Log = "/tmp/" + item.Slug + ".log"
if err := facades.Orm().Query().Create(&task); err != nil {
facades.Log().Error("[PHP-" + r.version + "] 创建卸载拓展任务失败:" + err.Error())
return controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误")
return controllers.ErrorSystem(ctx)
}

r.task.Process(task.ID)
Expand Down
18 changes: 9 additions & 9 deletions app/http/controllers/setting_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *SettingController) List(ctx http.Context) http.Response {
err := facades.Orm().Query().Get(&settings)
if err != nil {
facades.Log().Error("[面板][SettingController] 查询设置列表失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

type data struct {
Expand All @@ -51,7 +51,7 @@ func (r *SettingController) List(ctx http.Context) http.Response {
err = facades.Auth().User(ctx, &user)
if err != nil {
facades.Log().Error("[面板][SettingController] 获取用户失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
result.Username = user.Username
result.Email = user.Email
Expand Down Expand Up @@ -79,7 +79,7 @@ func (r *SettingController) Save(ctx http.Context) http.Response {
err := r.setting.Set(models.SettingKeyName, name)
if err != nil {
facades.Log().Error("[面板][SettingController] 保存设置失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
oldPort := tools.Exec(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`)
if oldPort != port {
Expand All @@ -91,7 +91,7 @@ func (r *SettingController) Save(ctx http.Context) http.Response {
err = r.setting.Set(models.SettingKeyBackupPath, backupPath)
if err != nil {
facades.Log().Error("[面板][SettingController] 保存设置失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
if !tools.Exists(websitePath) {
tools.Mkdir(websitePath, 0755)
Expand All @@ -100,19 +100,19 @@ func (r *SettingController) Save(ctx http.Context) http.Response {
err = r.setting.Set(models.SettingKeyWebsitePath, websitePath)
if err != nil {
facades.Log().Error("[面板][SettingController] 保存设置失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
err = r.setting.Set(models.SettingKeyEntrance, entrance)
if err != nil {
facades.Log().Error("[面板][SettingController] 保存设置失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

var user models.User
err = facades.Auth().User(ctx, &user)
if err != nil {
facades.Log().Error("[面板][SettingController] 获取用户失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

if len(username) > 0 {
Expand All @@ -125,14 +125,14 @@ func (r *SettingController) Save(ctx http.Context) http.Response {
hash, err := facades.Hash().Make(password)
if err != nil {
facades.Log().Error("[面板][SettingController] 保存设置失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}
user.Password = hash
}

if err = facades.Orm().Query().Save(&user); err != nil {
facades.Log().Error("[面板][SettingController] 保存设置失败 ", err)
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
return ErrorSystem(ctx)
}

return Success(ctx, nil)
Expand Down
Loading

0 comments on commit 00ec8d6

Please sign in to comment.