优化错误处理相关代码

This commit is contained in:
刘祥超
2023-08-11 14:51:23 +08:00
parent 2eee314ec8
commit 70d8507c4b
10 changed files with 16 additions and 17 deletions

View File

@@ -540,7 +540,7 @@ func (this *FileListDB) WrapError(err error) error {
if err == nil { if err == nil {
return nil return nil
} }
return errors.New(err.Error() + "(file: " + this.dbPath + ")") return fmt.Errorf("%w (file: %s)", err, this.dbPath)
} }
// 初始化 // 初始化

View File

@@ -4,7 +4,7 @@
package nftables package nftables
import ( import (
"errors" "fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const" teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/events" "github.com/TeaOSLab/EdgeNode/internal/events"
@@ -85,7 +85,7 @@ func (this *Installer) Install() error {
} }
// 检查是否已经存在 // 检查是否已经存在
if len(NftExePath()) > 0 { if len(NftExePath()) > 0 {
return nil return nil
} }
@@ -121,7 +121,7 @@ func (this *Installer) Install() error {
cmd.WithStderr() cmd.WithStderr()
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
return errors.New(err.Error() + ": " + cmd.Stderr()) return fmt.Errorf("%w: %s", err, cmd.Stderr())
} }
remotelogs.Println("NFTABLES", "installed nftables with command '"+cmd.String()+"' successfully") remotelogs.Println("NFTABLES", "installed nftables with command '"+cmd.String()+"' successfully")

View File

@@ -147,7 +147,7 @@ func (this *FirewalldAction) runActionSingleIP(action string, listType IPListTyp
cmd.WithStderr() cmd.WithStderr()
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
return errors.New(err.Error() + ", output: " + cmd.Stderr()) return fmt.Errorf("%w, output: %s", err, cmd.Stderr())
} }
return nil return nil
} }

View File

@@ -1,7 +1,7 @@
package iplibrary package iplibrary
import ( import (
"errors" "fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
@@ -128,7 +128,7 @@ func (this *IPTablesAction) runActionSingleIP(action string, listType IPListType
if strings.Contains(output, "No chain/target/match") { if strings.Contains(output, "No chain/target/match") {
err = nil err = nil
} else { } else {
return errors.New(err.Error() + ", output: " + output) return fmt.Errorf("%w, output: %s", err, output)
} }
} }
return nil return nil

View File

@@ -1,7 +1,6 @@
package iplibrary package iplibrary
import ( import (
"errors"
"fmt" "fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
@@ -62,7 +61,7 @@ func (this *ScriptAction) runAction(action string, listType IPListType, item *pb
cmd.WithStderr() cmd.WithStderr()
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
return errors.New(err.Error() + ", output: " + cmd.Stderr()) return fmt.Errorf("%w, output: %s", err, cmd.Stderr())
} }
return nil return nil
} }

View File

@@ -1,7 +1,7 @@
package nodes package nodes
import ( import (
"errors" "fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const" teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
@@ -141,7 +141,7 @@ func (this *ListenerManager) Start(nodeConfig *nodeconfigs.NodeConfig) error {
var port = addr[portIndex+1:] var port = addr[portIndex+1:]
var processName = this.findProcessNameWithPort(group.IsUDP(), port) var processName = this.findProcessNameWithPort(group.IsUDP(), port)
if len(processName) > 0 { if len(processName) > 0 {
err = errors.New(err.Error() + " (the process using port: '" + processName + "')") err = fmt.Errorf("%w (the process using port: '%s')", err, processName)
} }
} }
} }

View File

@@ -324,7 +324,7 @@ func (this *Node) syncConfig(taskVersion int64) error {
if os.IsNotExist(clusterErr) { if os.IsNotExist(clusterErr) {
return errors.New("can not find config file 'configs/api.yaml'") return errors.New("can not find config file 'configs/api.yaml'")
} }
return errors.New("check cluster config failed: " + clusterErr.Error()) return fmt.Errorf("check cluster config failed: %w", clusterErr)
} }
} else { } else {
return err return err

View File

@@ -4,7 +4,6 @@ package clock
import ( import (
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const" teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
@@ -136,7 +135,7 @@ func (this *ClockManager) syncNtpdate(ntpdate string, server string) error {
cmd.WithStderr() cmd.WithStderr()
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
return errors.New(err.Error() + ": " + cmd.Stderr()) return fmt.Errorf("%w: %s", err, cmd.Stderr())
} }
return nil return nil

View File

@@ -5,6 +5,7 @@ package utils
import ( import (
"errors" "errors"
"fmt"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const" teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec" executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
@@ -156,7 +157,7 @@ WantedBy=multi-user.target`
cmd.WithStderr() cmd.WithStderr()
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
return errors.New(err.Error() + ": " + cmd.Stderr()) return fmt.Errorf("%w: %s", err, cmd.Stderr())
} }
return nil return nil
} }

View File

@@ -2,13 +2,13 @@ package waf
import ( import (
"errors" "errors"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const" teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/waf/checkpoints" "github.com/TeaOSLab/EdgeNode/internal/waf/checkpoints"
"github.com/TeaOSLab/EdgeNode/internal/waf/requests" "github.com/TeaOSLab/EdgeNode/internal/waf/requests"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files" "github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/types"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"net/http" "net/http"
"os" "os"
@@ -96,7 +96,7 @@ func (this *WAF) Init() (resultErrors []error) {
err := group.Init(this) err := group.Init(this)
if err != nil { if err != nil {
// 这里我们不阻止其他规则正常加入 // 这里我们不阻止其他规则正常加入
resultErrors = append(resultErrors, errors.New("init group '"+types.String(group.Id)+"' failed: "+err.Error())) resultErrors = append(resultErrors, fmt.Errorf("init group '%d' failed: %w", group.Id, err))
} }
} }
} }