Skip to content

Commit

Permalink
feat: onResponseError support async
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Dec 11, 2024
1 parent d194f21 commit 7479e17
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ type LylaRequestOptions<C = undefined> = {
* fired by LylaError. Error thrown by user won't triggered the callback,
* for example if user throws an error in `onAfterResponse` hook. The
* callback won't be fired.
*
* Before the callback if finished, the error won't be thrown.
*/
onResponseError?: Array<
(error: LylaResponseError<C>, reject: (reason: unknown) => void) => void
(error: LylaResponseError<C>, reject: (reason: unknown) => void) => void | Promise<void>
>
/**
* Callbacks fired when a non-response error occurs (except
Expand Down
4 changes: 3 additions & 1 deletion README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ type LylaRequestOptions<C = undefined> = {
/**
* 响应处理遇到异常时的回调。只会在 LylaError 产生时被触发,用户触发的异常不会触发此回
* 调,例如用户在 `onAfterResponse` 回调中抛出异常不会触发该回调。
*
* 在次回调结束之前,异常不会被抛出
*/
onResponseError?: Array<
(error: LylaResponseError<C>, reject: (reason: unknown) => void) => void
(error: LylaResponseError<C>, reject: (reason: unknown) => void) => void | Promise<void>
>
/**
* 任何非 onResponseError 触发的错误都会触发次回调(除了 BROKEN_ON_NON_RESPONSE_ERROR)
Expand Down
13 changes: 4 additions & 9 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export function createLyla<C, M extends LylaAdapterMeta>(
await maybePromise
}
}
_reject(error)
} catch (e) {
_reject(
defineLylaError<M, C, LylaBrokenOnNonResponseErrorError<C, M>>(
Expand Down Expand Up @@ -287,6 +288,7 @@ export function createLyla<C, M extends LylaAdapterMeta>(
await maybePromise
}
}
_reject(error)
} catch (e) {
const brokenOnResponseErrorError = defineLylaError<
M,
Expand All @@ -305,7 +307,6 @@ export function createLyla<C, M extends LylaAdapterMeta>(
undefined
)
handleNonResponseError(brokenOnResponseErrorError)
_reject(brokenOnResponseErrorError)
return
}
}
Expand Down Expand Up @@ -374,9 +375,8 @@ export function createLyla<C, M extends LylaAdapterMeta>(
},
stack
)
handleResponseError(abortError)
_reject(abortError)
adapterHandle.abort()
const settled = () => adapterHandle.abort()
handleResponseError(abortError).then(settled).catch(settled)
}

if (signal) {
Expand Down Expand Up @@ -410,7 +410,6 @@ export function createLyla<C, M extends LylaAdapterMeta>(
stack
)
handleResponseError(networkError)
_reject(networkError)
},
onDownloadProgress: onDownloadProgress
? (progress) => {
Expand Down Expand Up @@ -453,7 +452,6 @@ export function createLyla<C, M extends LylaAdapterMeta>(
undefined
)
handleNonResponseError(brokenOnHeadersReceived)
_reject(brokenOnHeadersReceived)
return
}
}
Expand Down Expand Up @@ -549,7 +547,6 @@ export function createLyla<C, M extends LylaAdapterMeta>(
stack
)
handleResponseError(httpError)
_reject(httpError)
return
}

Expand Down Expand Up @@ -581,7 +578,6 @@ export function createLyla<C, M extends LylaAdapterMeta>(
undefined
)
handleNonResponseError(brokenOnAfterResponseErrorError)
_reject(brokenOnAfterResponseErrorError)
return
}
}
Expand Down Expand Up @@ -610,7 +606,6 @@ export function createLyla<C, M extends LylaAdapterMeta>(
stack
)
handleResponseError(timeoutError)
_reject(timeoutError)
}, timeout)
}
if (!_options.allowGetBody && method === 'GET' && body) {
Expand Down

0 comments on commit 7479e17

Please sign in to comment.