cloudevents.http.from_http binary incorrect error messages
还没有人认领这个 Issue。
评估
调研方向
从 cloudevents/http/http_methods.py 开始,沿着经过 cloudevents/http/event_type.py、cloudevents/sdk/converters/binary.py 和 cloudevents/sdk/converters/util.py 的二进制解析路径进行跟踪。复现 issue 中所示的缺少字段的情况,并验证缺少 ce-id、ce-source 或 ce-type 时,报告的是相应的缺少字段,而不是 specversion。
由索引模型根据 Issue 内容生成。
描述
Expected Behavior
When using cloudevents.http.from_http(headers, body), an event that is missing any of the required fields should result in a cloud_exceptions.MissingRequiredFields exception with a message that indicates which field is missing.
Actual Behavior
When given a Binary Cloud Event that is missing a required field ce-id, ce-source, or ce-type, it will return a MissingRequiredFields error with the incorrect error message Failed to find specversion in HTTP request.
Steps to Reproduce the Problem
from cloudevents.http import from_http
from cloudevents.exceptions import MissingRequiredFields
# Correctly does not result in an error if all required fields are present
def test_from_http():
event = from_http({"ce-specversion": "1.0", "ce-id":"123", "ce-type": "test-type", "ce-source": "test-source"}, "{}")
assert event["id"] == "123"
# Returns an incorrect error message
def test_from_http_missing_id_binary():
try:
event = from_http({"ce-specversion": "1.0", "ce-type": "test-type", "ce-source": "test-source"}, "{}")
assert 1 == 2
except MissingRequiredFields as e:
assert "Failed to find specversion in HTTP request" == str(e)
# Returns the appropriate message
def test_from_http_missing_id_structured():
try:
event = from_http({}, "{\"specversion\": \"1.0\", \"type\": \"test-type\", \"source\": \"test-source\"}")
assert 1 == 2
except MissingRequiredFields as e:
assert "Missing required attributes: {'id'}" == str(e)
The code flow is as follows:
- it checks
is_binary(headers)https://github.com/cloudevents/sdk-python/blob/b83bfc58eb851f9b91a96f4665754d9bb82cd74e/cloudevents/http/http_methods.py#L46 - It calls
binary_parser.can_readhttps://github.com/cloudevents/sdk-python/blob/b83bfc58eb851f9b91a96f4665754d9bb82cd74e/cloudevents/http/event_type.py#L6-L16 - it calls
has_binary_headershttps://github.com/cloudevents/sdk-python/blob/b83bfc58eb851f9b91a96f4665754d9bb82cd74e/cloudevents/sdk/converters/binary.py#L29-L35 has_binary_headerschecks for the presence of all required fields, which in this test case is false because it is missingce-idhttps://github.com/cloudevents/sdk-python/blob/b83bfc58eb851f9b91a96f4665754d9bb82cd74e/cloudevents/sdk/converters/util.py#L4-L10- it then falls through and tries to get the specversion here https://github.com/cloudevents/sdk-python/blob/b83bfc58eb851f9b91a96f4665754d9bb82cd74e/cloudevents/http/http_methods.py#L57
- specversion is never set and the error gets thrown here https://github.com/cloudevents/sdk-python/blob/b83bfc58eb851f9b91a96f4665754d9bb82cd74e/cloudevents/http/http_methods.py#L64-L67
I think that the solution might be as simple as changing all of the ands to ors in the has_binary_headers method in step 4.
Specifications
- Platform: Mac OS
- Python Version: 3.9.4
- 主要语言
- Python
- 星标
- 342
- 派生
- 65
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudevents/sdk-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
cloudevents/sdk-python#304 ·
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
cloudevents/sdk-python#300 ·
-
enhancement
难度 2/5 1-3 小时 新手友好度 68/100
cloudevents/sdk-python#291 · 8 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 48/100
cloudevents/sdk-python#247 · 2 条评论 ·
-
question
难度 3/5 1-2 天 新手友好度 45/100
cloudevents/sdk-python#246 · 5 条评论 ·
查看 cloudevents/sdk-python 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
stephrobert/dsoxlab#238 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
sublimehq/package_control#1780 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 70/100
nwg-piotr/nwg-displays#145 ·