cockroachdb / cockroachdb/errors

Behavior of HasType and HasInterface is counter-intuitive

Đang mở
#145 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Go
Star
2.5k
Fork
74
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Both of these functions call [`If`](https://sourcegraph.com/github.com/cockroachdb/errors@698c58cf81c4112204c35173983e95f2c7725de2/-/blob/markers/markers.go?L132-145) which does a traversal of the causal chain, it does not traverse the full tree. Specifically, `If` internally calls `UnwrapOnce` and doesn't handle the multi-error case, whereas functions like `Is` and `As` separately handle the multi-error case.

This leads to counter-intuitive behavior; you can have a value `x` of type `T`, and `errors.Is(err, x)` may be true, but `errors.HasType(err, T{})` may fail.

I noticed this behavior while trying to add property-based tests to better understand the behavior of `HasType` here. https://github.com/sourcegraph/sourcegraph/pull/62992

It would be valuable to either:
- Change the implementation of `If` to traverse the full tree
- OR Add a separate function which does a tree traversal (not just the "causal chain") and use that from `HasType` and `HasInterface`
- OR Add a cautionary warning to `HasType` and `HasInterface`'s docs which describe the behavior in the presence of multi-errors.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.