Made data null on error Made it remain in loading state on refetch and remount abortion
34 lines
896 B
TypeScript
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 }
|