Skip to content

Commit de6f093

Browse files
authored
feat: 检查网址时返回具体的报错信息 (#172)
close #167
1 parent eb2d22c commit de6f093

File tree

6 files changed

+66
-17
lines changed

6 files changed

+66
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
1111

1212
- 使用 jinja 渲染评论
1313
- 发布信息更新后如果发现不通过将拉取请求转成草稿
14+
- 检查网址时返回具体的报错信息
1415

1516
### Changed
1617

src/plugins/publish/templates/render_error.md.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
第 {{ loc[1] + 1 }} 个标签颜色错误<dt>请确保标签颜色符合十六进制颜色码规则。</dt>
1010
{%- elif loc|length == 2 and loc[0] == "tags" and type == "type_error.dict" %}
1111
第 {{ loc[1] + 1 }} 个标签格式错误。<dt>请确保标签为字典。</dt>
12-
{%- elif type == "value_error.homepage.unreachable" %}
12+
{%- elif type == "value_error.homepage" and error.ctx.status_code != -1 %}
1313
项目 <a href="{{ error.input }}">主页</a> 返回状态码 {{ error.ctx.status_code }}。<dt>请确保你的项目主页可访问。</dt>
14+
{%- elif type == "value_error.homepage" and error.ctx.status_code == -1 %}
15+
项目 <a href="{{ error.input }}">主页</a> 访问出错。<details><summary>错误信息</summary>{{ error.ctx.msg }}</details>
1416
{%- elif type == "value_error.project_link.not_found" %}
1517
项目 <a href="https://pypi.org/project/{{ error.input }}/">{{ error.input }}</a> 未发布至 PyPI。<dt>请将你的项目发布至 PyPI。</dt>
1618
{%- elif type == "value_error.project_link.name" %}

src/utils/validation/errors.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
class HomepageError(PydanticValueError):
55
code = "homepage"
6+
msg_template = "项目主页无法访问。"
67

7-
8-
class HomepageUnreachableError(HomepageError):
9-
code = "homepage.unreachable"
10-
msg_template = "项目主页不可访问。"
8+
def __init__(self, status_code: int, msg: str) -> None:
9+
super().__init__(status_code=status_code, msg=msg)
1110

1211

1312
class ModuleNameError(PydanticValueError):

src/utils/validation/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ErrorDict(PydanticErrorDict, total=False):
2222
)
2323
from .errors import (
2424
DuplicationError,
25-
HomepageUnreachableError,
25+
HomepageError,
2626
ModuleNameError,
2727
PluginSupportedAdaptersMissingError,
2828
PluginTypeError,
@@ -117,9 +117,9 @@ class PublishInfo(abc.ABC, BaseModel):
117117
@validator("homepage", pre=True)
118118
def homepage_validator(cls, v: str) -> str:
119119
if v:
120-
status_code = check_url(v)
120+
status_code, msg = check_url(v)
121121
if status_code != 200:
122-
raise HomepageUnreachableError(status_code=status_code)
122+
raise HomepageError(status_code=status_code, msg=msg)
123123
return v
124124

125125
@validator("tags", pre=True)

src/utils/validation/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
def check_pypi(project_link: str) -> bool:
1111
"""检查项目是否存在"""
1212
url = f"https://pypi.org/pypi/{project_link}/json"
13-
status_code = check_url(url)
13+
status_code, _ = check_url(url)
1414
return status_code == 200
1515

1616

1717
@cache
18-
def check_url(url: str) -> int | None:
18+
def check_url(url: str) -> tuple[int, str]:
1919
"""检查网址是否可以访问
2020
21-
返回状态码,如果报错则返回 None
21+
返回状态码,如果报错则返回 -1
2222
"""
2323
logger.info(f"检查网址 {url}")
2424
try:
2525
r = httpx.get(url, follow_redirects=True)
26-
return r.status_code
27-
except:
28-
pass
26+
return r.status_code, ""
27+
except Exception as e:
28+
return -1, str(e)
2929

3030

3131
def get_adapters() -> set[str]:

tests/publish/render/test_render_error.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def test_render_error_bot(app: App):
2525
{
2626
"loc": ("homepage",),
2727
"msg": "项目主页不可访问。",
28-
"type": "value_error.homepage.unreachable",
28+
"type": "value_error.homepage",
2929
"ctx": {"status_code": 404},
3030
"input": "https://www.baidu.com",
3131
},
@@ -92,7 +92,7 @@ async def test_render_error_adapter(app: App):
9292
{
9393
"loc": ("homepage",),
9494
"msg": "项目主页不可访问。",
95-
"type": "value_error.homepage.unreachable",
95+
"type": "value_error.homepage",
9696
"ctx": {"status_code": 404},
9797
"input": "https://www.baidu.com",
9898
},
@@ -158,7 +158,7 @@ async def test_render_error_plugin(app: App, mocker: MockFixture):
158158
{
159159
"loc": ("homepage",),
160160
"msg": "项目主页不可访问。",
161-
"type": "value_error.homepage.unreachable",
161+
"type": "value_error.homepage",
162162
"ctx": {"status_code": 404},
163163
"input": "https://www.baidu.com",
164164
},
@@ -400,3 +400,50 @@ async def test_render_unknown_error(app: App, mocker: MockFixture):
400400
comment
401401
== """# 📃 商店发布检查结果\n\n> Plugin: 帮助\n\n**⚠️ 在发布检查过程中,我们发现以下问题:**\n\n<pre><code><li>⚠️ tests &gt; 2 &gt; test: unknown error</li></code></pre>\n\n<details>\n<summary>详情</summary>\n<pre><code><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>\n</details>\n\n---\n\n💡 如需修改信息,请直接修改 issue,机器人会自动更新检查结果。\n💡 当插件加载测试失败时,请发布新版本后在当前页面下评论任意内容以触发测试。\n\n\n💪 Powered by [NoneFlow](https://github.com/nonebot/noneflow)\n<!-- NONEFLOW -->\n"""
402402
)
403+
404+
405+
async def test_render_http_error(app: App, mocker: MockFixture):
406+
"""网络请求报错"""
407+
from src.plugins.publish.config import plugin_config
408+
from src.plugins.publish.render import render_comment
409+
from src.utils.validation import PublishType, ValidationDict
410+
411+
mocker.patch.object(plugin_config, "plugin_test_result", True)
412+
mocker.patch.object(plugin_config, "plugin_test_metadata", {})
413+
414+
result: ValidationDict = {
415+
"valid": False,
416+
"data": {
417+
"author": "he0119",
418+
"tags": [],
419+
"is_official": False,
420+
},
421+
"errors": [
422+
{
423+
"loc": ("homepage",),
424+
"msg": "项目主页不可访问。",
425+
"type": "value_error.homepage",
426+
"ctx": {"status_code": 404},
427+
"input": "https://www.baidu.com",
428+
},
429+
{
430+
"loc": ("homepage",),
431+
"msg": "项目主页无法访问。",
432+
"type": "value_error.homepage",
433+
"ctx": {
434+
"status_code": -1,
435+
"msg": "Request URL is missing an 'http://' or 'https://' protocol.",
436+
},
437+
"input": "12312",
438+
},
439+
],
440+
"type": PublishType.PLUGIN,
441+
"name": "帮助",
442+
"author": "he0119",
443+
}
444+
445+
comment = await render_comment(result)
446+
assert (
447+
comment
448+
== """# 📃 商店发布检查结果\n\n> Plugin: 帮助\n\n**⚠️ 在发布检查过程中,我们发现以下问题:**\n\n<pre><code><li>⚠️ 项目 <a href="https://www.baidu.com">主页</a> 返回状态码 404。<dt>请确保你的项目主页可访问。</dt></li><li>⚠️ 项目 <a href="12312">主页</a> 访问出错。<details><summary>错误信息</summary>Request URL is missing an &#39;http://&#39; or &#39;https://&#39; protocol.</details></li></code></pre>\n\n<details>\n<summary>详情</summary>\n<pre><code><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>\n</details>\n\n---\n\n💡 如需修改信息,请直接修改 issue,机器人会自动更新检查结果。\n💡 当插件加载测试失败时,请发布新版本后在当前页面下评论任意内容以触发测试。\n\n\n💪 Powered by [NoneFlow](https://github.com/nonebot/noneflow)\n<!-- NONEFLOW -->\n"""
449+
)

0 commit comments

Comments
 (0)