Skip to content

Commit 27702db

Browse files
committed
fix: improve error handling in ConfigurationWrapper and useAsyncError hooks
1 parent 5f1baad commit 27702db

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/features/remoteConfig/providers/ConfigurationWrapper.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
2+
import BeatLoader from 'react-spinners/BeatLoader'
23
import { useAsyncError } from 'src/hooks/useAsyncError'
34
import { useGetRemoteConfig } from '../api/getRemoteConfig'
4-
import BeatLoader from 'react-spinners/BeatLoader'
55

66
type ConfigurationWrapperProps = {
77
children: React.ReactNode
@@ -11,6 +11,7 @@ export const ConfigurationWrapper = ({ children }: ConfigurationWrapperProps) =>
1111
const {
1212
isLoading,
1313
isError,
14+
error,
1415
data: remoteConfig,
1516
} = useGetRemoteConfig({
1617
config: {
@@ -29,7 +30,9 @@ export const ConfigurationWrapper = ({ children }: ConfigurationWrapperProps) =>
2930
}
3031

3132
if (isError || !remoteConfig) {
32-
throwError('Could not fetch configuration data, Make sure you are connected to the internet')
33+
throwError(
34+
error ?? 'Could not fetch configuration data, Make sure you are connected to the internet.'
35+
)
3336
}
3437

3538
return <>{children}</>

src/hooks/useAsyncError.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useCallback, useState } from 'react'
22

33
export const useAsyncError = () => {
4-
const [, setError] = useState()
5-
return useCallback(
6-
(e: string) => {
7-
setError(() => {
8-
throw e
9-
})
10-
},
11-
[setError]
12-
)
4+
const [, setError] = useState<never>()
5+
6+
return useCallback((err: unknown) => {
7+
setError(() => {
8+
if (err instanceof Error) {
9+
throw err
10+
}
11+
throw new Error(typeof err === 'string' ? err : JSON.stringify(err))
12+
})
13+
}, [])
1314
}

0 commit comments

Comments
 (0)