Skip to content

Commit 247d499

Browse files
fix: 修复在使用useRequest方法时,在onBefore配置项中手动抛出错误时会导致整个代码流程中断的问题(#177) (#179)
* feat: useFullscreen hook支持默认参数。并且同步更新文档与demo。 * feat: useFullscreen新增配置defaultElement项 * fix: useFullscreen defaultElement类型提示错误修复 * docs: 更正 useDrop 文档错误 * fix: 修复在使用useRequest方法时,在onBefore配置项中手动抛出错误时会导致整个代码流程中断的问题(#177)
1 parent 83ca206 commit 247d499

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

packages/hooks/src/useRequest/Fetch.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
116116
)
117117
// Do you want to stop the request
118118
if (stopNow) {
119-
return new Promise(() => { })
119+
return new Promise(() => {})
120120
}
121121

122122
this.setState({
@@ -130,8 +130,22 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
130130
return Promise.resolve(state.data)
131131
}
132132

133-
// Return before request
134-
this.options.onBefore?.(params)
133+
// The 'onBefore' configuration item error no longer interrupts the entire code flow
134+
try {
135+
// Return before request
136+
this.options.onBefore?.(params)
137+
} catch (error) {
138+
// The 'onBefore' configuration item error no longer interrupts the entire code flow
139+
this.setState({
140+
error,
141+
loading: false,
142+
})
143+
this.options.onError?.(error as Error, params)
144+
this.runPluginHandler('onError', error, params)
145+
146+
// Manually intercept the error and return a Promise with an empty status
147+
return new Promise(() => {})
148+
}
135149

136150
try {
137151
// Start the request with the replace service, if it contains the onRequest event name
@@ -140,7 +154,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
140154
const requestReturnResponse = (res: any) => {
141155
// The request has been cancelled, and the count will be inconsistent with the currentCount
142156
if (currentCount !== this.count) {
143-
return new Promise(() => { })
157+
return new Promise(() => {})
144158
}
145159
// Format data
146160
const formattedResult = this.options.formatResult ? this.options.formatResult(res) : res
@@ -174,7 +188,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
174188
return requestReturnResponse(servicePromiseResult)
175189
} catch (error) {
176190
if (currentCount !== this.count) {
177-
return new Promise(() => { })
191+
return new Promise(() => {})
178192
}
179193

180194
this.setState({

0 commit comments

Comments
 (0)