dm1sh b12f19ac51
Fixed useSend loading flow on abort
Made data null on error
Made it remain in loading state on refetch and remount abortion
2023-08-12 01:32:02 +03:00

34 lines
896 B
TypeScript

const isAborted = <T>(err: Error): err is AbortError<T> => (
err.name === 'AbortError'
)
type AbortErrorMessage = 'resent' | 'unmount' | 'cancel'
class AbortError<T> extends DOMException {
readonly fallback: T | undefined
message: AbortErrorMessage
constructor(message: AbortErrorMessage, fallback?: T) {
super(message, 'AbortError')
this.message = message
this.fallback = fallback
}
}
function handleHTTPErrors(res: Response) {
if (!res.ok) {
switch (res.status) {
case 401:
throw new Error('Ошибка авторизации')
case 404:
throw new Error('Объект не найден')
default: {
throw new Error('Ошибка ответа от сервера')
}
}
}
}
export { isAborted, AbortError, handleHTTPErrors }