Skip to content

Commit

Permalink
fix: 修复在使用useRequest方法时,在onBefore配置项中手动抛出错误时会导致整个代码流程中断的问题(#177) (#179)
Browse files Browse the repository at this point in the history
* feat: useFullscreen hook支持默认参数。并且同步更新文档与demo。

* feat: useFullscreen新增配置defaultElement项

* fix: useFullscreen defaultElement类型提示错误修复

* docs: 更正 useDrop 文档错误

* fix: 修复在使用useRequest方法时,在onBefore配置项中手动抛出错误时会导致整个代码流程中断的问题(#177)
  • Loading branch information
XiaoDaiGua-Ray authored Dec 24, 2023
1 parent 83ca206 commit 247d499
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/hooks/src/useRequest/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
)
// Do you want to stop the request
if (stopNow) {
return new Promise(() => { })
return new Promise(() => {})
}

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

// Return before request
this.options.onBefore?.(params)
// The 'onBefore' configuration item error no longer interrupts the entire code flow
try {
// Return before request
this.options.onBefore?.(params)
} catch (error) {
// The 'onBefore' configuration item error no longer interrupts the entire code flow
this.setState({
error,
loading: false,
})
this.options.onError?.(error as Error, params)
this.runPluginHandler('onError', error, params)

// Manually intercept the error and return a Promise with an empty status
return new Promise(() => {})
}

try {
// Start the request with the replace service, if it contains the onRequest event name
Expand All @@ -140,7 +154,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
const requestReturnResponse = (res: any) => {
// The request has been cancelled, and the count will be inconsistent with the currentCount
if (currentCount !== this.count) {
return new Promise(() => { })
return new Promise(() => {})
}
// Format data
const formattedResult = this.options.formatResult ? this.options.formatResult(res) : res
Expand Down Expand Up @@ -174,7 +188,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
return requestReturnResponse(servicePromiseResult)
} catch (error) {
if (currentCount !== this.count) {
return new Promise(() => { })
return new Promise(() => {})
}

this.setState({
Expand Down

0 comments on commit 247d499

Please sign in to comment.