-
Notifications
You must be signed in to change notification settings - Fork 597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fuo协议Show命令增强-Add Features #380
Changes from all commits
92db320
721ba12
6a1c925
7bdb046
34f4688
3d431d3
8a42fdb
89a0af0
a64deeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,22 @@ | |
from urllib.parse import parse_qsl, urlsplit | ||
|
||
|
||
class NotFound(Exception): | ||
class ShowCmdException(Exception): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 赞 |
||
pass | ||
|
||
|
||
# Rule not found | ||
class NotFound(ShowCmdException): | ||
def __init__(self, rule): | ||
self.rule = rule | ||
|
||
def __str__(self): | ||
# use backstrace lib (the format_exc/print_exc) causes fail | ||
# maybe the coroutine issue ? | ||
uri_msg = "Bad Uri Exception: fuo://{}".format(self.rule) | ||
return uri_msg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我觉得可以直接在抛错的时候把文字内容加进去,比如 raise NotFound(f"Bad Uri Exception: fuo://{self.rule}") |
||
|
||
|
||
def match(url, rules): | ||
"""找到 path 对应的 rule,并解析其中的参数 | ||
|
||
|
@@ -30,7 +42,8 @@ def match(url, rules): | |
query = dict(parse_qsl(qs)) | ||
params = match.groupdict() | ||
return rule, params, query | ||
raise NotFound | ||
|
||
raise NotFound(path) | ||
|
||
|
||
def _validate_rule(rule): | ||
|
@@ -96,7 +109,13 @@ def wrapper(*args, **kwargs): | |
return decorator | ||
|
||
def dispatch(self, uri, ctx): | ||
rule, params, query = match(uri, self.rules) | ||
try: | ||
rule, params, query = match(uri, self.rules) | ||
except NotFound: | ||
# pass it to the exception handle procedure in __init__.py | ||
raise | ||
return None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里只是 re-raise 了一下,看起来 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 对,代码删改到后面,变成这一段只是为了使得__init__.py能捕获得异常的信息,所以就简单的re-raise,应该有更好的方法来着 |
||
|
||
handler = self.handlers[rule] | ||
req = Request(uri=uri, rule=rule, params=params, query=query, ctx=ctx) | ||
return handler(req, **params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
像这种已经被正确处理了的错误,打个 warning/error 级别就可以了。
打 exception 的话,一般是意料之外的错误。比如未知的错误等。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
谢谢,学习到了