Skip to content
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

fix(): try to reload when specified error occurs #4588

Merged
merged 1 commit into from
Jan 3, 2025

Conversation

weareoutman
Copy link
Member

@weareoutman weareoutman commented Jan 2, 2025

INFRA-0

依赖检查

组件之间的依赖声明,是微服务组件架构下的重要信息,请确保其正确性。

请勾选以下两组选项其中之一:

  • 本次 MR 没有使用上游组件(例如框架、后台组件等)的较新版本提供的特性。

或者:

  • 本次 MR 使用了上游组件(例如框架、后台组件等)的较新版本提供的特性。
  • 在对应的文件中更新了该上游组件的依赖版本(或确认了当前声明的依赖版本已包含本次 MR 使用的新特性)。

提交信息检查

Git 提交信息将决定包的版本发布及自动生成的 CHANGELOG,请检查工作内容与提交信息是否相符,并在以下每组选项中都依次确认。

破坏性变更是针对于下游使用者而言,可以通过本次改动对下游使用者的影响来识别变更类型:

  • 下游使用者不做任何改动,仍可以正常工作时,那么它属于普通变更。
  • 反之,下游使用者不做改动就无法正常工作时,那么它属于破坏性变更。

例如,构件修改了一个属性名,小产品 Storyboard 中需要使用新属性名才能工作,那么它就是破坏性变更。
又例如,构件还没有任何下游使用者,那么它的任何变更都是普通变更。

破坏性变更:

  • ⚠️ 本次 MR 包含破坏性变更的提交,请继续确认以下所有选项:
  • 没有更好的兼容方案,必须做破坏性变更。
  • 使用了 feat 作为提交类型。
  • 标注了 BREAKING CHANGE: 你的变更说明
  • 同时更新了本仓库中所有下游使用者的调用。
  • 同时更新了本仓库中所有下游使用者对该子包的依赖为即将发布的 major 版本。
  • 同时为其它仓库的 Migrating 做好了准备,例如文档或批量改动的方法。
  • 手动验证过破坏性变更在 Migrate 后可以正常工作。
  • 破坏性变更所在的提交没有意外携带其它子包的改动。

新特性:

  • 本次 MR 包含新特性的提交,且该提交不带有破坏性变更,并使用了 feat 作为提交类型。
  • 给新特性添加了单元测试。
  • 手动验证过新特性可以正常工作。

问题修复:

  • 本次 MR 包含问题修复的提交,且该提交不带有新特性或破坏性变更,并使用了 fix 作为提交类型。
  • 给问题修复添加了单元测试。
  • 手动验证过问题修复得到解决。

杂项工作:

即所有对下游使用者无任何影响、且没有必要显示在 CHANGELOG 中的改动,例如修改注释、测试用例、开发文档等:

  • 本次 MR 包含杂项工作的提交,且该提交不带有问题修复、新特性或破坏性变更,并使用了 chore, docs, test 等作为提交类型。

Summary by CodeRabbit

  • 新功能

    • 新增网络错误检测和处理机制
    • 增加了在特定错误情况下自动重新加载页面的能力
  • Bug 修复

    • 优化了引导和路由过程中的错误处理逻辑
  • 本地化

    • 新增网络错误的中英文错误提示
  • 依赖更新

    • 更新 http-proxy-middleware^3.0.3
    • 添加 @next-core/utils 依赖
  • 国际化

    • 新增网络错误的本地化文案支持

Copy link

coderabbitai bot commented Jan 2, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/brick-container/src/bootstrap.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "@next-core/eslint-config-next" to extend from. Please check that the name of the config is correct.

The config "@next-core/eslint-config-next" was referenced from the config file in "/.eslintrc".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

总览

这个拉取请求引入了一系列与运行时错误处理和网络错误相关的改进。主要变更包括在运行时模块中添加新的错误检测和重载函数,更新引导程序的错误处理逻辑,并在国际化文件中添加网络错误消息。这些修改旨在增强应用程序在遇到网络错误时的鲁棒性和用户体验。

变更

文件路径 变更摘要
etc/runtime.api.md 添加新的公共函数:isNetworkErrorresetReloadForErrorshouldReloadForError;标记某些函数为已弃用
packages/brick-container/package.json 更新 http-proxy-middleware 依赖版本;添加 @next-core/utils 依赖
packages/brick-container/src/bootstrap.ts 引入新的错误处理逻辑,根据网络错误类型设置错误标题,添加条件重载机制
packages/brick-container/src/i18n.ts 添加 NETWORK_ERROR 枚举值和对应的中英文本地化消息
packages/runtime/src/index.ts 导出 isNetworkErrorshouldReloadForError 模块
packages/runtime/src/internal/Router.ts 集成新的错误重载函数 resetReloadForErrorshouldReloadForError
packages/runtime/src/isNetworkError.ts 新增 isNetworkError 函数,用于检测网络相关错误
packages/runtime/src/shouldReloadForError.ts 添加 shouldReloadForErrorresetReloadForError 函数,实现基于网络错误的条件重载逻辑

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/runtime/src/shouldReloadForError.ts (1)

6-16: 谨慎使用 sessionStorage 计数机制
在网络错误时根据计数重载页面,逻辑较清晰。但请确保在其他场景下不会误写或误删 sessionStorage,以免影响用户体验。如果未来有更多场景需要依赖该计数,建议封装工具函数集中管理。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a66abb0 and c8e1679.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (10)
  • etc/runtime.api.md (3 hunks)
  • packages/brick-container/package.json (2 hunks)
  • packages/brick-container/src/bootstrap.ts (2 hunks)
  • packages/brick-container/src/i18n.ts (2 hunks)
  • packages/runtime/src/index.ts (1 hunks)
  • packages/runtime/src/internal/Router.ts (3 hunks)
  • packages/runtime/src/isNetworkError.spec.ts (1 hunks)
  • packages/runtime/src/isNetworkError.ts (1 hunks)
  • packages/runtime/src/shouldReloadForError.spec.ts (1 hunks)
  • packages/runtime/src/shouldReloadForError.ts (1 hunks)
🔇 Additional comments (34)
packages/runtime/src/isNetworkError.ts (2)

1-3: 导入依赖看起来合理
导入 HttpFetchErrorBrickLoadError 能够准确区分网络请求异常与模块加载异常,对后续逻辑十分有帮助。


4-14: 逻辑封装清晰,便于统一判断网络错误
该函数综合判断多种类型的错误(包括不同自定义错误和原生事件类型),能让上层调用对网络错误的识别更加一致,值得肯定。

packages/runtime/src/shouldReloadForError.ts (1)

18-20: 重置功能简洁易懂
resetReloadForError 函数对清除计数非常直接,如有更多逻辑需求可在此处拓展。

packages/brick-container/src/i18n.ts (3)

5-5: 新增枚举值命名明晰
新枚举值 NETWORK_ERROR 命名直观,能在错误处理逻辑中快速定位到网络异常场景。


15-15: 英文翻译准确
"Network Error" 可以帮助用户清楚辨识网络相关的问题,与其他错误文案风格保持一致。


23-23: 中文翻译准确
"网络错误" 与英文翻译含义一致,方便国内用户理解。

packages/runtime/src/index.ts (1)

44-45: 导出新模块增强可复用性
isNetworkErrorshouldReloadForError 统一通过主入口导出,为其他模块或外部系统使用提供了更加便捷的路径。

packages/runtime/src/isNetworkError.spec.ts (9)

1-2: 导入依赖的范围符合测试需求
这里引入了 HttpFetchErrorBrickLoadError,可保证对网络错误类型进行充分覆盖。


3-4: 确保路径引用的正确性
引用的 isNetworkError.js 测试目标文件路径正确,应与项目结构保持一致。


5-9: 测试用例命名规范和可读性良好
“should return true for BrickLoadError” 准确描述预期行为,测试断言明确。


11-14: 测试覆盖 HttpFetchError 场景
HttpFetchError 的断言与实际逻辑相符,达成网络错误判断的完整性。


16-20: ChunkLoadError 测试场景完整
通过修改 error.name 模拟特定错误名称,可验证此类异常的网络错误判定正确性。


22-27: DOM Event 对象的网络错误识别测试
Event 类型的判断充分体现了边界检测,且伪造 target 逻辑合理。


29-32: 对其他普通错误的排除逻辑
此处确保非网络错误返回 false,保证判断函数的负向覆盖场景。


34-37: 对 null 和 undefined 入参的安全校验
验证空值输入可避免意外报错,测试场景覆盖充分。


39-42: 非 Error 实例输入的断言
此处可防止误判任意对象为网络错误,增强兼容性与鲁棒性。

packages/runtime/src/shouldReloadForError.spec.ts (7)

1-2: 导入功能函数的可测性良好
成功导入 shouldReloadForError 及依赖 BrickLoadError


4-6: 本地存储的相关方法已被正确监听
利用 jest.spyOn 使得读写操作可追踪,测试精确度提升。


8-16: 测试环境初始化配置完善
beforeEach 中设置 userAgent 与模拟 getItem 返回值,有助于测试独立与干净。


17-22: 测试网络错误且计数未达上限的重载逻辑
断言返回 true 并调用 setItem,符合预期,确保了计数的正确累加。


24-32: 测试计数达上限的场景
通过指向模拟值 "2",验证计数已达上限且返回 false,并断言移除计数项,逻辑清晰。


34-44: 测试与特定 userAgent 匹配的依赖关系
userAgent 不符合条件时,函数正确返回 false,且不更新计数。


46-51: 区分非网络错误的场景
确认返回 false 并且未更新计数,保证对常规错误不触发重载。

packages/brick-container/src/bootstrap.ts (5)

6-7: 新增引入重载逻辑的函数
在此处引入 shouldReloadForErrorresetReloadForError,为后续错误处理提供基础。


9-9: 同步引入 isNetworkError
增强基于错误类型的判定,在后续可针对网络错误赋予更精确的文案。


141-141: 重置重载计数
调用 resetReloadForError 后,可避免成功初始化后仍遗留上次错误的计数。


147-151: 实现特定错误触发页面刷新
shouldReloadForError 判断为需刷新时,直接调用 location.reload() 并返回 failed,逻辑简明。


158-160: 细化网络错误提示文案
通过 isNetworkError 判断使用 NETWORK_ERROR 文案,加深用户对错误类型的理解。

packages/runtime/src/internal/Router.ts (3)

59-62: 引入重载功能函数
新增对 resetReloadForErrorshouldReloadForError 的导入,提升路由错误的可控性。


433-436: 在路由调用栈中判定特定错误后触发刷新
当满足 shouldReloadForError 的条件则执行刷新,保证对网络级别重载需求的即时响应。


517-517: 重置错误计数的最佳时机
在渲染流程结束时统一调用 resetReloadForError,有效防止意外循环重载。

packages/brick-container/package.json (2)

45-45: 依赖版本更新看起来合理

http-proxy-middleware 的小版本更新(3.0.2 -> 3.0.3)应该包含了一些bug修复。


65-65: 新增依赖关系配置正确

@next-core/utils 作为新依赖同时被添加到 devDependencies 和 peerDependencies 中,这符合微服务架构中的最佳实践。

Also applies to: 71-71

etc/runtime.api.md (1)

253-255: 新增网络错误检测函数

isNetworkError 函数的添加有助于统一处理网络相关错误,这是一个很好的抽象。

etc/runtime.api.md Show resolved Hide resolved
Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.21%. Comparing base (a66abb0) to head (c91b3d2).
Report is 2 commits behind head on v3.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##               v3    #4588   +/-   ##
=======================================
  Coverage   95.20%   95.21%           
=======================================
  Files         206      208    +2     
  Lines        8940     8958   +18     
  Branches     1707     1711    +4     
=======================================
+ Hits         8511     8529   +18     
  Misses        321      321           
  Partials      108      108           
Files with missing lines Coverage Δ
packages/brick-container/src/i18n.ts 100.00% <ø> (ø)
packages/runtime/src/internal/Router.ts 95.11% <100.00%> (+0.08%) ⬆️
packages/runtime/src/isNetworkError.ts 100.00% <100.00%> (ø)
packages/runtime/src/shouldReloadForError.ts 100.00% <100.00%> (ø)

Copy link

cypress bot commented Jan 2, 2025

next-core    Run #10858

Run Properties:  status check passed Passed #10858  •  git commit 7d7f43a108 ℹ️: Merge c91b3d264f1e8ed09e575864ea7aefacb71307c9 into a66abb00c5ef6e134f4b864bee87...
Project next-core
Branch Review steve/v3-reload-for-error
Run status status check passed Passed #10858
Run duration 00m 22s
Commit git commit 7d7f43a108 ℹ️: Merge c91b3d264f1e8ed09e575864ea7aefacb71307c9 into a66abb00c5ef6e134f4b864bee87...
Committer Shenwei Wang
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 16
View all changes introduced in this branch ↗︎

@weareoutman weareoutman force-pushed the steve/v3-reload-for-error branch from c8e1679 to c91b3d2 Compare January 2, 2025 13:52
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
etc/runtime.api.md (1)

528-530: 建议完善错误重载机制的文档

shouldReloadForError 和 resetReloadForError 函数组合使用,为错误处理提供了灵活的重载机制。建议:

  1. 在文档中添加使用示例
  2. 说明重载的触发条件和限制
packages/runtime/src/internal/Runtime.spec.ts (1)

668-676: myTimeoutProvider 新增字符串类型 fail 参数。
fail 扩展为字符串能更灵活地模拟不同错误场景。可考虑在文档或注释中补充说明参数含义,以便他人快速理解。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8e1679 and c91b3d2.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (11)
  • etc/runtime.api.md (3 hunks)
  • packages/brick-container/package.json (2 hunks)
  • packages/brick-container/src/bootstrap.ts (2 hunks)
  • packages/brick-container/src/i18n.ts (2 hunks)
  • packages/runtime/src/index.ts (1 hunks)
  • packages/runtime/src/internal/Router.ts (3 hunks)
  • packages/runtime/src/internal/Runtime.spec.ts (7 hunks)
  • packages/runtime/src/isNetworkError.spec.ts (1 hunks)
  • packages/runtime/src/isNetworkError.ts (1 hunks)
  • packages/runtime/src/shouldReloadForError.spec.ts (1 hunks)
  • packages/runtime/src/shouldReloadForError.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (9)
  • packages/runtime/src/index.ts
  • packages/brick-container/src/i18n.ts
  • packages/runtime/src/shouldReloadForError.ts
  • packages/runtime/src/isNetworkError.ts
  • packages/runtime/src/internal/Router.ts
  • packages/brick-container/package.json
  • packages/runtime/src/shouldReloadForError.spec.ts
  • packages/runtime/src/isNetworkError.spec.ts
  • packages/brick-container/src/bootstrap.ts
🧰 Additional context used
🪛 Biome (1.9.4)
packages/runtime/src/internal/Runtime.spec.ts

[error] 1744-1745: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

🔇 Additional comments (7)
etc/runtime.api.md (2)

253-255: 函数签名设计合理

函数使用 unknown 类型作为参数,符合 TypeScript 的类型安全最佳实践。


401-403: 需要补充 resetReloadForError 的测试用例

根据之前的评审记录,resetReloadForError 函数缺少测试覆盖。建议添加单元测试以确保其功能的正确性和稳定性。

packages/runtime/src/internal/Runtime.spec.ts (5)

4-4: 引入新错误类型以增强错误处理逻辑。
该导入在本文件启用对 BrickLoadError 的测试,可读性和可维护性都很清晰。


19-19: 引入 shouldReloadForError 模块。
这个导入用于决定在出现特定错误时是否执行页面重载,逻辑上与后续测试用例相吻合。


26-26: shouldReloadForError 进行 jest.mock 的注意事项。
此处的 mock 能模拟不同错误触发页面重载的行为,但需谨慎以防止测试结果被错误的模拟逻辑干扰。


585-602: brick-load-error 路由新增上下文配置。
这里在运行时环境中为“砖块加载失败”场景引入了新的路由配置,并模拟了 fail === "BrickLoadError" 时抛出的异常。测试覆盖面显著增强。


1741-1775: 测试“在出现砖块加载错误后自动重载”的场景。
此处测试了若 shouldReloadForError 返回真值,页面将通过 window.location.reload 强制刷新。该逻辑与设计目标一致,但请保持对错误类型(如 BrickLoadError)的清楚区分并在后续维护中小心处理跳转、缓存等问题。

🧰 Tools
🪛 Biome (1.9.4)

[error] 1744-1745: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

packages/runtime/src/internal/Runtime.spec.ts Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 12 changed files in this pull request and generated no comments.

Files not reviewed (6)
  • packages/brick-container/package.json: Language not supported
  • etc/runtime.api.md: Evaluated as low risk
  • packages/brick-container/src/bootstrap.ts: Evaluated as low risk
  • packages/brick-container/src/i18n.ts: Evaluated as low risk
  • packages/runtime/src/index.ts: Evaluated as low risk
  • packages/runtime/src/internal/Router.ts: Evaluated as low risk
const MAX_RELOAD_COUNT = 2;

export function shouldReloadForError(error: unknown): boolean {
if (/upchat/i.test(navigator.userAgent) && isNetworkError(error)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仅该标识的客户端网络有异常,针对性处理

@qiaofengxi qiaofengxi merged commit bcabcee into v3 Jan 3, 2025
7 checks passed
@qiaofengxi qiaofengxi deleted the steve/v3-reload-for-error branch January 3, 2025 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants