File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -1084,6 +1084,23 @@ describe('useFetch - BROWSER - errors', (): void => {
1084
1084
expect ( result . current . data ) . toEqual ( [ ] )
1085
1085
} )
1086
1086
1087
+ it ( 'should set the `error` with custom errors' , async ( ) : Promise < void > => {
1088
+ const customError = { id : 'Custom Error' }
1089
+ fetch . resetMocks ( )
1090
+ fetch . mockResponse ( ( ) => Promise . reject ( customError ) )
1091
+ const { result } = renderHook (
1092
+ ( ) => useFetch ( 'https://example.com' , {
1093
+ data : [ ] ,
1094
+ cachePolicy : NO_CACHE
1095
+ } )
1096
+ )
1097
+ expect ( result . current . data ) . toEqual ( [ ] )
1098
+ expect ( result . current . loading ) . toBe ( false )
1099
+ await act ( result . current . get )
1100
+ expect ( result . current . error ) . toEqual ( customError )
1101
+ expect ( result . current . data ) . toEqual ( [ ] )
1102
+ } )
1103
+
1087
1104
it ( 'should reset the error after each call' , async ( ) : Promise < void > => {
1088
1105
fetch . resetMocks ( )
1089
1106
fetch . mockRejectOnce ( expectedError )
Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
84
84
body ,
85
85
interceptors . request
86
86
)
87
-
87
+
88
88
error . current = undefined
89
89
90
90
// don't perform the request if there is no more data to fetch (pagination)
@@ -151,8 +151,10 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
151
151
const theData = await retry ( opts , routeOrBody , body )
152
152
return theData
153
153
}
154
- if ( err . name !== 'AbortError' ) error . current = makeError ( err . name , err . message )
155
-
154
+ if ( err . name !== 'AbortError' ) {
155
+ error . current = err
156
+ }
157
+
156
158
} finally {
157
159
timedout . current = false
158
160
if ( timer ) clearTimeout ( timer )
You can’t perform that action at this time.
0 commit comments