AlexStocks commented on code in PR #980:
URL: https://github.com/apache/dubbo-go-pixiu/pull/980#discussion_r4505148266
##########
pkg/common/extension/filter/filter.go:
##########
@@ -157,39 +157,40 @@ var (
// OnDecode empty implement
func (enf *EmptyNetworkFilter) OnDecode(data []byte) (any, int, error) {
- panic("OnDecode is not implemented")
+ return nil, 0, errors.New("EmptyNetworkFilter: OnDecode not
implemented")
Review Comment:
💭 `(nil, 0, err)` 这个形态是对的(与 `NetworkFilterChain.OnDecode` 里的 `return nil, 0,
errors.Errorf("filterChain don't have network filter")` 保持一致),但 `0` 作为 length
会被调用方原样传给 getty:
```go
result, length, decodeErr = fc.OnDecode(data) //
pkg/listener/tcp/pkg_handler.go:41
...
return result, length, err // -> getty Read
```
getty 在 err != nil 时不会用 length,所以现在没问题。但如果将来有人在中间加一层「吞掉错误只记日志」的处理,`length=0`
就会被当成「需要 0 字节的完整包」——那是个很难查的协议层故障。
建议在 `EmptyNetworkFilter` 的字段注释里写明「所有 not-implemented 方法返回的 length/status
都没有意义,调用方必须在 err != nil 时忽略它」,或者把这几个方法集中到一个 `errNotImplemented` 哨兵错误上,方便调用方
`errors.Is` 判断「这是未实现,不是协议错误」。
##########
admin/dao/daos.go:
##########
@@ -22,7 +22,7 @@ type GuestDao interface {
// Login login
Login(username, password string) (bool, int)
// CheckLogin check login
- CheckLogin()
+ CheckLogin() error
Review Comment:
💭 这是导出接口的签名变更(`CheckLogin()` → `CheckLogin() error`)。仓库内实现只有
`admin/dao/impl/guest.go` 一处,所以编译没问题;但外部实现者会直接编译失败。
既然改了就顺手把它做完整:现在是「未实现就返回固定错误」,调用方拿到 err
之后大概率和「校验失败」走同一个分支。建议让错误可判别,避免上层把「功能没实现」误报成「登录无效」:
```go
// admin/dao/impl/guest.go
var ErrNotImplemented = errors.New("guest dao: session validation is not
implemented")
func (d *GuestDao) CheckLogin() error { return ErrNotImplemented }
```
上层就能 `errors.Is(err, impl.ErrNotImplemented)` 时返回 501/Not Implemented,而不是
401/Unauthorized。差别在于运维看监控时能不能区分「有人调用了未实现的能力」和「凭据不对」。
##########
pkg/client/mq/mq.go:
##########
@@ -83,7 +83,7 @@ type Client struct {
}
func (c Client) Apply() error {
- panic("implement me")
+ return perrors.New("mq: Apply not implemented")
Review Comment:
💭 抛错是对的(不再是 panic),但这条错误会在每次启动时以 ERROR 级别打出来:`server/adapter_manager.go:79`
是 `logger.Errorf("initFilterIfNeed apply adapter error %s", err)`,而 adapter 无论
Apply 是否成功都会 `ads = append(ads, hf)`。
两个可选处理:
1. 把「能力未实现」从错误降级为一个可判别的哨兵,让 `initAdapters` 对这类错误用 `Warn`(`errors.Is(err,
ErrNotImplemented)`);
2. 或者在 `initAdapters` 里根本不注册未实现的 adapter。
现在这样「每次启动一条 ERROR」的噪音,会让真正的启动错误更难被注意到。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]