Skip to content

onBeforeRequest 报错时支持走重试逻辑#499

Open
bao-io wants to merge 1 commit into
imroc:masterfrom
bao-io:beforeRetry
Open

onBeforeRequest 报错时支持走重试逻辑#499
bao-io wants to merge 1 commit into
imroc:masterfrom
bao-io:beforeRetry

Conversation

@bao-io

@bao-io bao-io commented Jun 28, 2026

Copy link
Copy Markdown

背景

当前 OnBeforeRequest 中间件一旦返回 error,Request.do() 会直接 return,不会走 RetryCondition / RetryHook。重试逻辑只在请求发出之后才会触发。
但有些逻辑需要在请求发出前执行(例如获取 token、预检查),失败时同样希望能按 RetryCondition 判断是否重试。

改动

  • 抽出 shouldRetryprepareRetrytryRetry,复用请求后的重试逻辑
  • udBeforeRequest 报错时调用 tryRetry,满足条件则 continue retry
  • 在调用 RetryCondition 前先构造 resp(含 RequestErr),保证 hook 里能拿到 resp.Request,即使请求还没发到网络

行为说明

  • 仅覆盖用户中间件 udBeforeRequest,内置 beforeRequest(如 parseRequestURL)报错仍直接返回
  • RetryCondition 返回 falseMaxRetries 耗尽、context.Canceled 等行为与原来一致

测试

  • TestRetryOnBeforeRequestError
  • TestRetryConditionHasRequestOnBeforeRequestError
  • TestNoRetryOnBeforeRequestErrorWhenConditionFalse

Adds functionality to retry requests when encountering errors during the before-request phase. Introduces methods to determine if a retry should occur based on custom conditions and hooks. Enhances the existing retry mechanism to handle temporary errors effectively, ensuring robust request handling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant