enhance: replace error handling with cosy wrapper for nginx reload and test failures

This commit is contained in:
Jacky 2025-04-18 08:06:34 +00:00
parent c5e80fb98a
commit d789326230
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
14 changed files with 57 additions and 43 deletions

View file

@ -76,9 +76,7 @@ func AddConfig(c *gin.Context) {
output := nginx.Reload() output := nginx.Reload()
if nginx.GetLogLevel(output) >= nginx.Warn { if nginx.GetLogLevel(output) >= nginx.Warn {
c.JSON(http.StatusInternalServerError, gin.H{ cosy.ErrHandler(c, cosy.WrapErrorWithParams(config.ErrNginxReloadFailed, output))
"message": output,
})
return return
} }

View file

@ -35,13 +35,6 @@ const isWorkspace = computed(() => {
<div class="tool"> <div class="tool">
<MenuUnfoldOutlined @click="emit('clickUnFold')" /> <MenuUnfoldOutlined @click="emit('clickUnFold')" />
</div> </div>
<div v-if="!isWorkspace" class="workspace-entry">
<RouterLink to="/workspace">
<ATooltip :title="$gettext('Workspace')">
<DesktopOutlined />
</ATooltip>
</RouterLink>
</div>
<ASpace <ASpace
class="user-wrapper" class="user-wrapper"
@ -51,6 +44,14 @@ const isWorkspace = computed(() => {
<SwitchAppearance /> <SwitchAppearance />
<div v-if="!isWorkspace" class="workspace-entry">
<RouterLink to="/workspace">
<ATooltip :title="$gettext('Workspace')">
<DesktopOutlined />
</ATooltip>
</RouterLink>
</div>
<Notification :header-ref="headerRef" /> <Notification :header-ref="headerRef" />
<NginxControl /> <NginxControl />
@ -98,8 +99,6 @@ const isWorkspace = computed(() => {
} }
.workspace-entry { .workspace-entry {
position: absolute;
left: 20px;
@media (max-width: 600px) { @media (max-width: 600px) {
display: none; display: none;
} }

View file

@ -6,4 +6,6 @@ var (
e = cosy.NewErrorScope("config") e = cosy.NewErrorScope("config")
ErrPathIsNotUnderTheNginxConfDir = e.New(50006, "path: {0} is not under the nginx conf dir: {1}") ErrPathIsNotUnderTheNginxConfDir = e.New(50006, "path: {0} is not under the nginx conf dir: {1}")
ErrDstFileExists = e.New(50007, "destination file: {0} already exists") ErrDstFileExists = e.New(50007, "destination file: {0} already exists")
ErrNginxTestFailed = e.New(50008, "nginx test failed: {0}")
ErrNginxReloadFailed = e.New(50009, "nginx reload failed: {0}")
) )

View file

@ -1,13 +1,13 @@
package config package config
import ( import (
"fmt"
"os" "os"
"github.com/0xJacky/Nginx-UI/internal/helper" "github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx" "github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query" "github.com/0xJacky/Nginx-UI/query"
"github.com/uozi-tech/cosy"
"gorm.io/gen/field" "gorm.io/gen/field"
) )
@ -47,7 +47,7 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
output := nginx.Reload() output := nginx.Reload()
if nginx.GetLogLevel(output) >= nginx.Warn { if nginx.GetLogLevel(output) >= nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
err = SyncToRemoteServer(cfg) err = SyncToRemoteServer(cfg)

View file

@ -2,15 +2,17 @@ package site
import ( import (
"fmt" "fmt"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy/logger"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"sync" "sync"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
) )
// Disable disables a site by removing the symlink in sites-enabled // Disable disables a site by removing the symlink in sites-enabled
@ -35,7 +37,7 @@ func Disable(name string) (err error) {
output := nginx.Reload() output := nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
go syncDisable(name) go syncDisable(name)

View file

@ -11,6 +11,7 @@ import (
"github.com/0xJacky/Nginx-UI/internal/nginx" "github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification" "github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
) )
@ -37,12 +38,12 @@ func Enable(name string) (err error) {
output := nginx.TestConf() output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
_ = os.Remove(enabledConfigFilePath) _ = os.Remove(enabledConfigFilePath)
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
} }
output = nginx.Reload() output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
go syncEnable(name) go syncEnable(name)

View file

@ -8,4 +8,6 @@ var (
ErrDstFileExists = e.New(50001, "destination file already exists") ErrDstFileExists = e.New(50001, "destination file already exists")
ErrSiteIsEnabled = e.New(50002, "site is enabled") ErrSiteIsEnabled = e.New(50002, "site is enabled")
ErrSiteIsInMaintenance = e.New(50003, "site is in maintenance mode") ErrSiteIsInMaintenance = e.New(50003, "site is in maintenance mode")
ErrNginxTestFailed = e.New(50004, "nginx test failed: {0}")
ErrNginxReloadFailed = e.New(50005, "nginx reload failed: {0}")
) )

View file

@ -16,6 +16,7 @@ import (
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/tufanbarisyildirim/gonginx/config" "github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/parser" "github.com/tufanbarisyildirim/gonginx/parser"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
cSettings "github.com/uozi-tech/cosy/settings" cSettings "github.com/uozi-tech/cosy/settings"
) )
@ -82,13 +83,13 @@ func EnableMaintenance(name string) (err error) {
if helper.FileExists(originalEnabledPath + "_backup") { if helper.FileExists(originalEnabledPath + "_backup") {
_ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath) _ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath)
} }
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
} }
// Reload nginx // Reload nginx
output = nginx.Reload() output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
// Synchronize with other nodes // Synchronize with other nodes

View file

@ -14,6 +14,7 @@ import (
"github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query" "github.com/0xJacky/Nginx-UI/query"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
) )
@ -40,13 +41,13 @@ func Save(name string, content string, overwrite bool, envGroupId uint64, syncNo
output := nginx.TestConf() output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
} }
if postAction == model.PostSyncActionReloadNginx { if postAction == model.PostSyncActionReloadNginx {
output = nginx.Reload() output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
} }
} }

View file

@ -2,15 +2,17 @@ package stream
import ( import (
"fmt" "fmt"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy/logger"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"sync" "sync"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
) )
// Disable disables a site by removing the symlink in sites-enabled // Disable disables a site by removing the symlink in sites-enabled
@ -35,7 +37,7 @@ func Disable(name string) (err error) {
output := nginx.Reload() output := nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
go syncDisable(name) go syncDisable(name)

View file

@ -2,15 +2,17 @@ package stream
import ( import (
"fmt" "fmt"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy/logger"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"sync" "sync"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
) )
// Enable enables a site by creating a symlink in sites-enabled // Enable enables a site by creating a symlink in sites-enabled
@ -36,12 +38,12 @@ func Enable(name string) (err error) {
output := nginx.TestConf() output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
_ = os.Remove(enabledConfigFilePath) _ = os.Remove(enabledConfigFilePath)
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
} }
output = nginx.Reload() output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
go syncEnable(name) go syncEnable(name)

View file

@ -7,4 +7,6 @@ var (
ErrStreamNotFound = e.New(40401, "stream not found") ErrStreamNotFound = e.New(40401, "stream not found")
ErrDstFileExists = e.New(50001, "destination file already exists") ErrDstFileExists = e.New(50001, "destination file already exists")
ErrStreamIsEnabled = e.New(50002, "stream is enabled") ErrStreamIsEnabled = e.New(50002, "stream is enabled")
ErrNginxTestFailed = e.New(50003, "nginx test failed: {0}")
ErrNginxReloadFailed = e.New(50004, "nginx reload failed: {0}")
) )

View file

@ -7,6 +7,7 @@ import (
"github.com/0xJacky/Nginx-UI/internal/notification" "github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/query" "github.com/0xJacky/Nginx-UI/query"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
"net/http" "net/http"
"os" "os"
@ -49,13 +50,13 @@ func Rename(oldName string, newName string) (err error) {
// test nginx configuration // test nginx configuration
output := nginx.TestConf() output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
} }
// reload nginx // reload nginx
output = nginx.Reload() output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
go syncRename(oldName, newName) go syncRename(oldName, newName)

View file

@ -14,6 +14,7 @@ import (
"github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query" "github.com/0xJacky/Nginx-UI/query"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
) )
@ -40,13 +41,13 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64, pos
output := nginx.TestConf() output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
} }
if postAction == model.PostSyncActionReloadNginx { if postAction == model.PostSyncActionReloadNginx {
output = nginx.Reload() output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn { if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output) return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
} }
} }
} }