Skip to content

Commit 33c2f90

Browse files
committed
Improve fetch error
1 parent c22bff9 commit 33c2f90

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

examples/basic/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import useFetch from "../../index";
44

55

66
const App = () => {
7-
const {isLoading, data} = useFetch(`https://swapi.co/api/people/1`);
7+
const {isLoading, data, error} = useFetch(`https://swapi.co/api/people/1`);
88

9-
console.log(isLoading, data);
9+
console.log(isLoading, data, error && error.status);
1010

1111
return <div>
1212
<p>isLoading: {isLoading && "true" || "false"}</p>

index.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22
declare namespace useFetch {
3+
export interface UseFetchError extends Error {
4+
status: number,
5+
statusText: string
6+
}
7+
38
export interface FetchResult<T> {
49
data?: T,
510
isLoading: boolean,
6-
error?: any
11+
error?: UseFetchError
712
}
813

914
export interface HookOptions extends RequestInit {

index.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
var usePromise = require('./usePromise')
22

3+
function UseFetchError (status, statusText, message, fileName, lineNumber) {
4+
var instance = new Error(message, fileName, lineNumber)
5+
instance.name = 'UseFetchError'
6+
instance.status = status
7+
instance.statusText = statusText
8+
Object.setPrototypeOf(instance, Object.getPrototypeOf(this))
9+
if (Error.captureStackTrace) {
10+
Error.captureStackTrace(instance, UseFetchError)
11+
}
12+
return instance
13+
}
14+
15+
UseFetchError.prototype = Object.create(Error.prototype, {
16+
constructor: {
17+
value: Error,
18+
enumerable: false,
19+
writable: true,
20+
configurable: true
21+
}
22+
})
23+
24+
Object.setPrototypeOf(UseFetchError, Error)
25+
326
function useFetch (
427
path,
528
options,
@@ -11,7 +34,13 @@ function useFetch (
1134
return usePromise(!blocked && function (p, o, s) {
1235
return fetch(p, o)
1336
.then((s && s.formatter) || (o && o.formatter) || function (response) {
14-
if (!response.ok) throw Error(response.statusText)
37+
if (!response.ok) {
38+
throw new UseFetchError(
39+
response.status,
40+
response.statusText,
41+
'Fetch error'
42+
)
43+
}
1544
return response.json()
1645
})
1746
},

index.js.flow

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
type TUseFetchResult<T> = {
44
data: ?T,
55
isLoading: boolean,
6-
error: mixed,
6+
error?: Error & {
7+
status: number,
8+
statusText: string
9+
}
710
};
811
declare function useFetch<T>(path: RequestInfo, options?: { ...RequestOptions,
912
formatter?: (Response) => Promise<T>,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"ignore": [
3131
"react"
3232
],
33-
"limit": "408 B",
33+
"limit": "556 B",
3434
"path": "index.js",
3535
"running": false
3636
}

0 commit comments

Comments
 (0)