-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/logging error handling 优化异常处理/日志打印逻辑 #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3d6713a
refactor: 抽离异常包装逻辑
YewFence b9ee251
refactor: 更改了检查方法的命名
YewFence 7b27a01
fix: 修复模块导入的问题
YewFence c80780f
fix: 删除冗余的关闭逻辑
YewFence 127d44c
refactor: 使用统一日志系统替换print输出
YewFence 24b96be
feat(logger): 添加日志轮转和第三方库日志过滤功能
YewFence e04a1d1
feat: 为cookie转换函数添加异常上下文装饰器
YewFence 2129e88
refactor(video): 移除日志消息中的Rich标记语法并修复异常类型
YewFence 9f787e3
feat: 添加浏览器关闭异常处理机制
YewFence 1fe83e5
feat: 添加键盘中断处理和程序运行日志
YewFence 851c47a
feat: 添加全局异常处理器记录未捕获的异常
YewFence dafc023
refactor: 统一浏览器关闭异常处理机制
YewFence 045c3c9
chore: 优化日志管理并清理调试文件
YewFence f9019b9
fix: 修复PR#9 review问题
YewFence File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,3 +28,6 @@ Thumbs.db | |
|
|
||
| # LLM | ||
| .claude/ | ||
|
|
||
| # Logs | ||
| log/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from functools import wraps | ||
| import asyncio | ||
|
|
||
|
|
||
| class BrowserClosedError(Exception): | ||
| """用户手动关闭浏览器时抛出的异常""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| def _is_browser_closed_error(e: BaseException) -> bool: | ||
| """检查是否为浏览器关闭相关的错误""" | ||
| if isinstance(e, BrowserClosedError): | ||
| return True | ||
| # 检查异常链中是否包含浏览器关闭错误 | ||
| if e.__cause__ and _is_browser_closed_error(e.__cause__): | ||
| return True | ||
| # 检查错误消息 | ||
| # 不依赖 Playwright 的内部异常类型;用消息匹配即可覆盖“目标已关闭”等场景。 | ||
| error_msg = ( | ||
| (str(e) or "").lower() | ||
| + " " | ||
| + ("".join(map(str, getattr(e, "args", ()))) or "").lower() | ||
| ) | ||
| if any( | ||
| keyword in error_msg | ||
| for keyword in [ | ||
| "target page, context or browser has been closed", | ||
| "browser has been closed", | ||
| ] | ||
| ): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def exception_context(step_name): | ||
| def decorator(func): | ||
| # 检测是否为异步函数 | ||
| if asyncio.iscoroutinefunction(func): | ||
|
|
||
| @wraps(func) | ||
| async def async_wrapper(*args, **kwargs): | ||
| try: | ||
| return await func(*args, **kwargs) | ||
| except Exception as e: | ||
| # 检查是否为浏览器关闭错误 | ||
| if _is_browser_closed_error(e): | ||
| raise BrowserClosedError("用户已关闭浏览器") from None | ||
| # 统一包装异常 | ||
| raise RuntimeError(f"{step_name}时发生异常") from e | ||
|
|
||
| return async_wrapper | ||
| else: | ||
|
|
||
| @wraps(func) | ||
| def sync_wrapper(*args, **kwargs): | ||
| try: | ||
| return func(*args, **kwargs) | ||
| except Exception as e: | ||
| # 检查是否为浏览器关闭错误 | ||
| if _is_browser_closed_error(e): | ||
| raise BrowserClosedError("用户已关闭浏览器") from None | ||
| # 统一包装异常 | ||
| raise RuntimeError(f"{step_name}时发生异常") from e | ||
|
|
||
| return sync_wrapper | ||
|
|
||
| return decorator | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Include
__context__when walking the exception chain.Exceptions raised without
fromstore the original in__context__, so browser-closed errors can be missed. Consider checking it alongside__cause__.🔧 Suggested update
# 检查异常链中是否包含浏览器关闭错误 if e.__cause__ and _is_browser_closed_error(e.__cause__): return True + if e.__context__ and _is_browser_closed_error(e.__context__): + return True🧰 Tools
🪛 Ruff (0.14.14)
19-19: Comment contains ambiguous
;(FULLWIDTH SEMICOLON). Did you mean;(SEMICOLON)?(RUF003)
🤖 Prompt for AI Agents