优化错误处理,替换 errors.Wrap 为 fmt.Errorf

This commit is contained in:
2025-05-15 09:53:23 +08:00
parent 75569d2d6d
commit 8b7dc9e4ff
11 changed files with 80 additions and 92 deletions

View File

@@ -1,6 +1,7 @@
package auth
import (
"fmt"
"log/slog"
"net"
"proxy-server/server/fwd/core"
@@ -9,7 +10,7 @@ import (
"strconv"
"time"
"github.com/pkg/errors"
"errors"
)
type Protocol string
@@ -25,7 +26,7 @@ func CheckIp(conn net.Conn, proto Protocol) (*core.AuthContext, error) {
remoteAddr := conn.RemoteAddr().String()
remoteHost, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
return nil, errors.Wrap(err, "noAuth 认证失败")
return nil, fmt.Errorf("无法获取连接信息: %w", err)
}
// 获取服务端口
@@ -33,7 +34,7 @@ func CheckIp(conn net.Conn, proto Protocol) (*core.AuthContext, error) {
_, _localPort, err := net.SplitHostPort(localAddr)
localPort, err := strconv.Atoi(_localPort)
if err != nil {
return nil, errors.Wrap(err, "noAuth 认证失败")
return nil, fmt.Errorf("noAuth 认证失败: %w", err)
}
// 查询权限记录
@@ -51,7 +52,7 @@ func CheckIp(conn net.Conn, proto Protocol) (*core.AuthContext, error) {
// 记录应该只有一条
channel, err := orm.MaySingle(channels)
if err != nil {
return nil, errors.Wrap(err, "不在白名单内")
return nil, errors.New("不在白名单内")
}
// 检查是否需要密码认证
@@ -80,7 +81,7 @@ func CheckPass(conn net.Conn, proto Protocol, username, password string) (*core.
_, _localPort, err := net.SplitHostPort(localAddr)
localPort, err := strconv.Atoi(_localPort)
if err != nil {
return nil, errors.Wrap(err, "noAuth 认证失败")
return nil, fmt.Errorf("noAuth 认证失败: %w", err)
}
// 查询权限记录
@@ -92,7 +93,7 @@ func CheckPass(conn net.Conn, proto Protocol, username, password string) (*core.
Protocol: string(proto),
}).Error
if err != nil {
return nil, errors.Wrap(err, "用户不存在")
return nil, errors.New("用户不存在")
}
// 检查密码 todo 哈希
@@ -107,7 +108,7 @@ func CheckPass(conn net.Conn, proto Protocol, username, password string) (*core.
remoteAddr := conn.RemoteAddr().String()
remoteHost, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
return nil, errors.Wrap(err, "无法获取连接信息")
return nil, fmt.Errorf("无法获取连接信息: %w", err)
}
// 查询权限记录