mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 20:40:25 +08:00
优化错误提示相关代码
This commit is contained in:
@@ -5,7 +5,7 @@ package iplibrary
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
@@ -45,7 +45,7 @@ func NewFileDataReader(dataReader io.Reader, password string) (*FileReader, erro
|
||||
|
||||
gzReader, err := gzip.NewReader(dataReader)
|
||||
if err != nil {
|
||||
return nil, errors.New("create gzip reader failed: " + err.Error())
|
||||
return nil, fmt.Errorf("create gzip reader failed: %w", err)
|
||||
}
|
||||
|
||||
reader, err := NewReader(gzReader)
|
||||
|
||||
@@ -4,6 +4,7 @@ package iplibrary
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"io"
|
||||
@@ -76,7 +77,7 @@ func (this *Updater) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("read ip library file failed '" + err.Error() + "'")
|
||||
return fmt.Errorf("read ip library file failed '%w'", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = fp.Close()
|
||||
@@ -167,7 +168,7 @@ func (this *Updater) Loop() error {
|
||||
// write to file
|
||||
fp, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
return errors.New("create ip library file failed: " + err.Error())
|
||||
return fmt.Errorf("create ip library file failed: %w", err)
|
||||
}
|
||||
|
||||
var isOk = false
|
||||
@@ -195,7 +196,7 @@ func (this *Updater) Loop() error {
|
||||
err = this.loadFile(fp)
|
||||
_ = fp.Close()
|
||||
if err != nil {
|
||||
return errors.New("load file failed: " + err.Error())
|
||||
return fmt.Errorf("load file failed: %w", err)
|
||||
}
|
||||
|
||||
isOk = true
|
||||
@@ -215,7 +216,7 @@ func (this *Updater) loadFile(fp *os.File) error {
|
||||
|
||||
fileReader, err := NewFileDataReader(fp, "")
|
||||
if err != nil {
|
||||
return errors.New("load ip library from reader failed: " + err.Error())
|
||||
return fmt.Errorf("load ip library from reader failed: %w", err)
|
||||
}
|
||||
|
||||
var reader = fileReader.RawReader()
|
||||
@@ -227,7 +228,7 @@ func (this *Updater) loadFile(fp *os.File) error {
|
||||
func (this *Updater) createDefaultFile(sourcePath string, dir string) error {
|
||||
sourceFp, err := os.Open(sourcePath)
|
||||
if err != nil {
|
||||
return errors.New("prepare to copy file to 'ip-library.db' failed: " + err.Error())
|
||||
return fmt.Errorf("prepare to copy file to 'ip-library.db' failed: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = sourceFp.Close()
|
||||
@@ -235,14 +236,14 @@ func (this *Updater) createDefaultFile(sourcePath string, dir string) error {
|
||||
|
||||
dstFp, err := os.Create(dir + "/ip-library.db")
|
||||
if err != nil {
|
||||
return errors.New("prepare to copy file to 'ip-library.db' failed: " + err.Error())
|
||||
return fmt.Errorf("prepare to copy file to 'ip-library.db' failed: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = dstFp.Close()
|
||||
}()
|
||||
_, err = io.Copy(dstFp, sourceFp)
|
||||
if err != nil {
|
||||
return errors.New("copy file to 'ip-library.db' failed: " + err.Error())
|
||||
return fmt.Errorf("copy file to 'ip-library.db' failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package langs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"strings"
|
||||
)
|
||||
@@ -51,7 +52,7 @@ func (this *Lang) Compile() error {
|
||||
for code, oldMessage := range this.messageMap {
|
||||
message, err := this.get(code, 0)
|
||||
if err != nil {
|
||||
return errors.New("compile '" + string(code) + "': '" + oldMessage + "' failed: " + err.Error())
|
||||
return fmt.Errorf("compile '%s': '%s' failed: %w", string(code), oldMessage, err)
|
||||
}
|
||||
this.messageMap[code] = message
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"time"
|
||||
)
|
||||
@@ -26,7 +27,7 @@ func EncryptMap(nodeUniqueId string, nodeSecret string, data maps.Map, timeout i
|
||||
"data": data,
|
||||
})
|
||||
if err != nil {
|
||||
return "", errors.New("marshal data to json failed: " + err.Error())
|
||||
return "", fmt.Errorf("marshal data to json failed: %w", err)
|
||||
}
|
||||
|
||||
var method = &AES256CFBMethod{}
|
||||
@@ -52,7 +53,7 @@ func DecryptMap(nodeUniqueId string, nodeSecret string, encodedString string) (m
|
||||
|
||||
encodedData, err := base64.StdEncoding.DecodeString(encodedString)
|
||||
if err != nil {
|
||||
return nil, errors.New("base64 decode failed: " + err.Error())
|
||||
return nil, fmt.Errorf("base64 decode failed: %w", err)
|
||||
}
|
||||
|
||||
dataJSON, err := method.Decrypt(encodedData)
|
||||
@@ -63,7 +64,7 @@ func DecryptMap(nodeUniqueId string, nodeSecret string, encodedString string) (m
|
||||
var result = maps.Map{}
|
||||
err = json.Unmarshal(dataJSON, &result)
|
||||
if err != nil {
|
||||
return nil, errors.New("unmarshal data failed: " + err.Error())
|
||||
return nil, fmt.Errorf("unmarshal data failed: %w", err)
|
||||
}
|
||||
|
||||
var expiresAt = result.GetInt64("expiresAt")
|
||||
@@ -107,7 +108,7 @@ func DecryptData(nodeUniqueId string, nodeSecret string, encodedString string) (
|
||||
|
||||
encodedData, err := base64.StdEncoding.DecodeString(encodedString)
|
||||
if err != nil {
|
||||
return nil, errors.New("base64 decode failed: " + err.Error())
|
||||
return nil, fmt.Errorf("base64 decode failed: %w", err)
|
||||
}
|
||||
|
||||
return method.Decrypt(encodedData)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"strings"
|
||||
@@ -20,13 +20,13 @@ func HumanError(err error, endpoints []string, configFile string) (resultErr err
|
||||
}
|
||||
switch errStatus.Code() {
|
||||
case codes.InvalidArgument:
|
||||
return errors.New("错误的RPC参数:" + err.Error()), false
|
||||
return fmt.Errorf("错误的RPC参数:%w", err), false
|
||||
case codes.DeadlineExceeded:
|
||||
return errors.New("RPC操作超时,请重试:" + err.Error()), false
|
||||
return fmt.Errorf("RPC操作超时,请重试:%w", err), false
|
||||
case codes.Unimplemented:
|
||||
return errors.New("请求的RPC服务或方法不存在,可能是没有升级API节点或者当前节点没有升级:" + err.Error()), false
|
||||
return fmt.Errorf("请求的RPC服务或方法不存在,可能是没有升级API节点或者当前节点没有升级:%w", err), false
|
||||
case codes.Unavailable:
|
||||
return errors.New("RPC当前不可用:<br/>1、请确认当前节点的api.yaml(<em>" + configFile + "</em>)配置中的地址(<em>" + strings.Join(endpoints, ", ") + "</em>)是否已填写正确;<br/>2、请确保API节点已启动,并检查当前节点和API节点之间的网络连接是正常的。<hr/>错误信息:" + err.Error()), true
|
||||
return fmt.Errorf("RPC当前不可用:<br/>1、请确认当前节点的api.yaml(<em>%s</em>)配置中的地址(<em>%s</em>)是否已填写正确;<br/>2、请确保API节点已启动,并检查当前节点和API节点之间的网络连接是正常的。<hr/>错误信息:%w", configFile, strings.Join(endpoints, ", "), err), true
|
||||
}
|
||||
|
||||
return err, false
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package firewallconfigs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
@@ -28,12 +28,12 @@ func (this *HTTPFirewallRule) Init() error {
|
||||
case HTTPFirewallRuleOperatorMatch:
|
||||
_, err := regexp.Compile(this.Value)
|
||||
if err != nil {
|
||||
return errors.New("regexp validate failed: " + err.Error() + ", expression: " + this.Value)
|
||||
return fmt.Errorf("regexp validate failed: %w, expression: %s", err, this.Value)
|
||||
}
|
||||
case HTTPFirewallRuleOperatorNotMatch:
|
||||
_, err := regexp.Compile(this.Value)
|
||||
if err != nil {
|
||||
return errors.New("regexp validate failed: " + err.Error() + ", expression: " + this.Value)
|
||||
return fmt.Errorf("regexp validate failed: %w, expression: %s", err, this.Value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
@@ -112,7 +113,7 @@ func (this *SSLCertConfig) Init(ctx context.Context) error {
|
||||
} else { // 证书+私钥
|
||||
cert, err := tls.X509KeyPair(this.CertData, this.KeyData)
|
||||
if err != nil {
|
||||
return errors.New("load certificate '" + strconv.FormatInt(this.Id, 10) + "' failed:" + err.Error())
|
||||
return fmt.Errorf("load certificate '%s' failed: %w", strconv.FormatInt(this.Id, 10), err)
|
||||
}
|
||||
|
||||
for index, data := range cert.Certificate {
|
||||
|
||||
Reference in New Issue
Block a user